#include "mid_FSM.h"
#include "stdio.h"
#include "string.h"

static FSMGlobal g_FSMGlobal = {
    .Now_State = FSM_NONE,
    .Menu_ui_Process = FSM_Quick_Setup,
    .Cal_ui_Process = Cal_Analog_Pick,
    .Cal_ui_Parameter = 1,
    .Cal_ui_Cursor = 0,
    .Configure_ui_Process = FSM_Measurement,
    .System_ui_Process = FSM_Set_Language,
    .Service_ui_Process = FSM_Diagnostic,
    .Quick_ui_Process = Quick_parameter,
    .Quick_ui_Cursor = 0,
    .Quick_ui_Parameter = 0,
    .Quick_ui_Parameter2 = 0,
    .Quick_ui_Parameter3 = 0,
    .Channel_Setup_ui_Process = Channel_Setup_parameter,
    .Channel_Setup_ui_Cursor = 0,
    .Channel_Setup_ui_Parameter = 0,
    .Channel_Setup_ui_Parameter2 = 0,
    .Channel_Setup_ui_Parameter3 = 0,
    .Channel_Setup_ui_Parameter4 = 0,
};

FSMData g_FSMData = {
    .Cal_Aout_Num = 1,
    .Cal_Aout1_4mA = 7777,
    .Cal_Aout1_20mA = 8888,
    .Cal_Aout2_4mA = 9999,
    .Cal_Aout2_20mA = 11111,
    .Quick_Num = 0,           // 快速设置参数通道选择
    .Quick_Unit = 0,          // 快速设置参数单位
    .Quick_analog_output = 0, // 快速设置模拟输出使能

    .Channel_Setup_a_measurements = 0,    // a通道选择测量值
    .Channel_Setup_a_unit_multiplier = 0, // a通道选择单位乘数
    .Channel_Setup_b_measurements = 0,    // b通道选择测量值
    .Channel_Setup_b_unit_multiplier = 0, // b通道选择单位乘数
    .Channel_Setup_c_measurements = 0,    // c通道选择测量值
    .Channel_Setup_c_unit_multiplier = 0, // c通道选择单位乘数
    .Channel_Setup_d_measurements = 0,    // d通道选择测量值
    .Channel_Setup_d_unit_multiplier = 0, // d通道选择单位乘数
};

FSMData g_FSMData_temp = {
    .Cal_Aout_Num = 1,
    .Cal_Aout1_4mA = 7777,
    .Cal_Aout1_20mA = 8888,
    .Cal_Aout2_4mA = 9999,
    .Cal_Aout2_20mA = 11111,
    .Quick_Num = 0,           // 快速设置参数通道选择
    .Quick_Unit = 0,          // 快速设置参数单位
    .Quick_analog_output = 0, // 快速设置模拟输出使能

    .Channel_Setup_a_measurements = 0,    // a通道选择测量值
    .Channel_Setup_a_unit_multiplier = 0, // a通道选择单位乘数
    .Channel_Setup_b_measurements = 0,    // b通道选择测量值
    .Channel_Setup_b_unit_multiplier = 0, // b通道选择单位乘数
    .Channel_Setup_c_measurements = 0,    // c通道选择测量值
    .Channel_Setup_c_unit_multiplier = 0, // c通道选择单位乘数
    .Channel_Setup_d_measurements = 0,    // d通道选择测量值
    .Channel_Setup_d_unit_multiplier = 0, // d通道选择单位乘数
};

TFT_UIManager g_UIManager;
UI_Parameter_t g_UI_Parameter;
UI_process_t g_UI_process_t[FSM_UI_COUNT];

bool Loop_flag = true;
const int power_of_10[] = {1, 10, 100, 1000, 10000};

char *FSM_State_Strings[FSM_UI_COUNT + 1] = {
    "NONE",                    // FSM_NONE
    "MENU",                    // FSM_Menu
    "Cal",                     // FSM_Cal
    "Info",                    // FSM_Info
    "Quick Setup",             // FSM_Quick_Setup
    "Configure",               // FSM_Configure
    "System",                  // FSM_System
    "Service",                 // FSM_Service
    "Messages",                // FSM_Messages
    "Calibration Data",        // FSM_Calibration_Data
    "Model Software Revision", // FSM_Model_Software_Revision
    "TOC Sensor Info",         // FSM_TOC_Sensor_Info
    "Measurement",             // FSM_Measurement
    "Analog Outputs",          // FSM_Analog_Outputs
    "Set Points",              // FSM_Set_Points
    "Alarm",                   // FSM_Alarm
    "TOC Setup",               // FSM_TOC_Setup
    "Display",                 // FSM_Display
    "Flow Control",            // FSM_Flow_Contro1
    "Hold outputs",            // FSM_Hold_outputs
    "Set Language",            // FSM_Set_Language
    "USB",                     // FSM_USB
    "Passwords",               // FSM_Passwords
    "Set Clear Lockout",       // FSM_Set_Clear_Lockout
    "Reset",                   // FSM_Reset
    "Diagnostic",              // FSM_Diagnostic
    "Calibrate",               // FSM_Calibrate
    "Tech Service",            // FSM_Tech_Service

    "Channel Setup", // FSM_Channel_Setup
    "Resistivity",   // FSM_Resistivity
    "Set Averaging", // FSM_Set_Averaging

    "STATE COUNT" // FSM_STATE_COUNT
};

typedef enum
{
    meas_toc = 0,
    meas_gc_L,
    meas_O_cm,
    meas_S_m,
    meas_S_cm,
    meas_oC,
    meas_oF,
    meas_LampRemaining,
    meas_Nothing,

    meas_STATE_COUNT, // 状态总数
} TOC_measurements;

char *TOC_measurements_Strings[meas_STATE_COUNT + 1] = {
    "TOC",
    "gC/L",
    "O-cm",
    "S/m",
    "S/cm",
    " C",
    " F",
    "LampRemaining",
    "Nothing",

    " meas_STATE COUNT" // meas_STATE_COUNT
};
char str[50] = {0};

void Celsius_display(uint16_t x, uint8_t line);

void TFT_menu_line_write(uint8_t line, uint16_t x_start, const char *str)
{
    tft.setTextSize(TFT_TEXTSIZE);
    uint16_t y_pos = TFT_UI_START_Y;

    if (line == 2)
    {
        y_pos += TFT_TEXT_LENGTH;
    }

    tft.drawString(str, x_start + TFT_UI_START_X, y_pos);
}

FSM_UI get_FSM_UI(void)
{
    return g_FSMGlobal.Now_State;
}

// 写菜单第一行
void TFT_menu_first_line_write(uint16_t x_start, char *str)
{
    TFT_menu_line_write(1, x_start, str);
}
// 写菜单第二行
void TFT_menu_second_line_write(uint16_t x_start, char *str)
{
    TFT_menu_line_write(2, x_start, str);
}
// 写菜单箭头
void TFT_menu_Arrow_write(char *str)
{
    tft.setTextSize(TFT_TEXTSIZE);
    tft.drawString(str, 280, TFT_UI_START_Y + TFT_TEXT_LENGTH);
}

// 以字为单位写字符串
void Menu_WriteStr(uint8_t y_line, uint8_t x_char_start, char *str)
{
    TFT_menu_line_write(y_line, x_char_start * TFT_TEXT_WIDTH, str);
}

// 以字为单位写字符串
void Menu_WriteChar(uint8_t y_line, uint8_t x_char_start, char Writechar)
{
    uint16_t y_pos = TFT_UI_START_Y;

    if (y_line == 2)
    {
        y_pos += TFT_TEXT_LENGTH;
    }
    tft.drawChar(Writechar, TFT_UI_START_X + x_char_start * TFT_TEXT_WIDTH, y_pos, tft.fontcolor);
}

// 以字为单位清除字符
void Menu_ClearChar(uint8_t y_line, uint8_t x_char_start, uint8_t char_num)
{
    uint16_t y_pos = TFT_UI_START_Y;

    if (y_line == 2)
    {
        y_pos += TFT_TEXT_LENGTH;
    }
    tft.fillRect(TFT_UI_START_X + (x_char_start)*TFT_TEXT_WIDTH, y_pos, char_num * TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
}

// 清除菜单第一行
void TFT_menu_first_line_Clear(uint16_t x_start, uint16_t x_len)
{
    tft.fillRect(x_start + TFT_UI_START_X, TFT_UI_START_Y, x_len, TFT_TEXT_LENGTH, 0);
}
// 清除菜单第二行
void TFT_menu_second_line_Clear(uint16_t x_start, uint16_t x_len)
{
    tft.fillRect(x_start + TFT_UI_START_X, TFT_UI_START_Y + TFT_TEXT_LENGTH, x_len, TFT_TEXT_LENGTH, 0);
}
// 清除菜单箭头
void TFT_menu_Arrow_Clear(void)
{
    tft.fillRect(280, TFT_UI_START_Y + TFT_TEXT_LENGTH, 30, TFT_TEXT_LENGTH, 0);
}

// 清除所有
void TFT_menu_ALL_Clear(void)
{
    TFT_menu_first_line_Clear(0, 285);
    TFT_menu_second_line_Clear(0, 265);
    TFT_menu_Arrow_Clear();
}

void FSM_NONE_handler(void)
{
    //    float num = 31.8f;
    //    static uint32_t last_press_time = 0;
    //    uint32_t current_time = HAL_GetTick();

    //    if ((current_time - last_press_time) >= 1000)
    //    {
    //        TFT_menu_first_line_Clear(12 * TFT_TEXT_WIDTH, 6 * TFT_TEXT_WIDTH);
    //        sprintf(str, "%.1f", 25.6f);
    //        TFT_menu_first_line_write(12 * TFT_TEXT_WIDTH, str);
    //        Celsius_display(18 * TFT_TEXT_WIDTH, 1);

    //        TFT_menu_second_line_Clear(12 * TFT_TEXT_WIDTH, 6 * TFT_TEXT_WIDTH);
    //        sprintf(str, "%.1f", 31.8f);
    //        TFT_menu_second_line_write(12 * TFT_TEXT_WIDTH, str);
    //        Celsius_display(18 * TFT_TEXT_WIDTH, 2);

    //        last_press_time = current_time;
    //    }
}

void FSM_Menu_handler(void)
{
    static uint32_t last_press_time = 0;

    uint32_t current_time = HAL_GetTick();

    if ((current_time - last_press_time) >= 500)
    {
        if (Loop_flag)
        {
            TFT_menu_first_line_write(0, FSM_State_Strings[g_FSMGlobal.Now_State]);
            TFT_menu_second_line_write(0, FSM_State_Strings[g_FSMGlobal.Menu_ui_Process]);
            TFT_menu_Arrow_write("<-");
            Loop_flag = false;
        }
        else
        {
            TFT_menu_second_line_Clear(0, 265);
            Loop_flag = true;
        }

        last_press_time = current_time;
    }
}

void FSM_Cal_handler(void)
{
    static uint32_t last_press_time = 0;
    uint32_t current_time = HAL_GetTick();

    if ((current_time - last_press_time) >= 500)
    {
        switch (g_FSMGlobal.Cal_ui_Process)
        {
        case Cal_Analog_Pick:

            if (Loop_flag)
            {
                if (g_FSMGlobal.Cal_ui_Cursor == 1)
                {
                    TFT_menu_Arrow_write("<-");
                }
                else
                {
                    tft.fillRect(TFT_UI_START_X + 14 * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
                    tft.drawChar(g_FSMGlobal.Cal_ui_Parameter + '0', TFT_UI_START_X + 14 * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, tft.fontcolor);
                }
                Loop_flag = false;
            }
            else
            {
                if (g_FSMGlobal.Cal_ui_Cursor == 1)
                {
                    TFT_menu_Arrow_Clear();
                }
                else
                {
                    tft.fillRect(TFT_UI_START_X + 14 * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
                    tft.drawChar('_', TFT_UI_START_X + 14 * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, tft.fontcolor);
                }
                Loop_flag = true;
            }
            break;
        case Cal_4mA:
            if (Loop_flag)
            {
                if (g_FSMGlobal.Cal_ui_Cursor == 5)
                {
                    TFT_menu_Arrow_write("<-");
                }
                else
                {
                    // const int power_of_10[] = {1, 10, 100, 1000, 10000};
                    int temp = (g_FSMGlobal.Cal_ui_Parameter / power_of_10[4 - g_FSMGlobal.Cal_ui_Cursor]) % 10;

                    tft.fillRect(TFT_UI_START_X + (14 + g_FSMGlobal.Cal_ui_Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
                    tft.drawChar(temp + '0', TFT_UI_START_X + (14 + g_FSMGlobal.Cal_ui_Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
                }
                Loop_flag = false;
            }
            else
            {
                if (g_FSMGlobal.Cal_ui_Cursor == 5)
                {
                    TFT_menu_Arrow_Clear();
                }
                else
                {
                    tft.fillRect(TFT_UI_START_X + (14 + g_FSMGlobal.Cal_ui_Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
                    tft.drawChar('_', TFT_UI_START_X + (14 + g_FSMGlobal.Cal_ui_Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
                }
                Loop_flag = true;
            }
            break;
        case Cal_20mA:
            if (Loop_flag)
            {
                if (g_FSMGlobal.Cal_ui_Cursor == 5)
                {
                    TFT_menu_Arrow_write("<-");
                }
                else
                {
                    // const int power_of_10[] = {1, 10, 100, 1000, 10000};
                    int temp = (g_FSMGlobal.Cal_ui_Parameter / power_of_10[4 - g_FSMGlobal.Cal_ui_Cursor]) % 10;

                    tft.fillRect(TFT_UI_START_X + (15 + g_FSMGlobal.Cal_ui_Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
                    tft.drawChar(temp + '0', TFT_UI_START_X + (15 + g_FSMGlobal.Cal_ui_Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
                }
                Loop_flag = false;
            }
            else
            {
                if (g_FSMGlobal.Cal_ui_Cursor == 5)
                {
                    TFT_menu_Arrow_Clear();
                }
                else
                {
                    tft.fillRect(TFT_UI_START_X + (15 + g_FSMGlobal.Cal_ui_Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
                    tft.drawChar('_', TFT_UI_START_X + (15 + g_FSMGlobal.Cal_ui_Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
                }
                Loop_flag = true;
            }
            break;
        case Cal_Save_Enable:
            if (Loop_flag)
            {
                if (g_FSMGlobal.Cal_ui_Cursor == 1)
                {
                    TFT_menu_Arrow_write("<-");
                }
                else
                {
                    // const int power_of_10[] = {1, 10, 100, 1000, 10000};
                    //                    int temp = (g_FSMGlobal.Cal_ui_Parameter / power_of_10[4 - g_FSMGlobal.Cal_ui_Cursor]) % 10;

                    tft.fillRect(TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH * 3, TFT_TEXT_LENGTH, 0);
                    if (g_FSMGlobal.Cal_ui_Parameter == 0)
                    {
                        tft.drawString("Yes", TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
                    }
                    else
                    {
                        tft.drawString("No", TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
                    }
                }
                Loop_flag = false;
            }
            else
            {
                if (g_FSMGlobal.Cal_ui_Cursor == 1)
                {
                    TFT_menu_Arrow_Clear();
                }
                else
                {
                    tft.fillRect(TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH * 3, TFT_TEXT_LENGTH, 0);
                    tft.drawChar('_', TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
                }
                Loop_flag = true;
            }
            break;

        default:
            break;
        }

        last_press_time = current_time;
    }
}
// 绘制Quick_Analog光标画面
void Quick_Analog_draw(int8_t Cursor)
{
    switch (Cursor)
    {
    case 0:
        tft.fillRect(TFT_UI_START_X, TFT_UI_START_Y, TFT_TEXT_WIDTH, 20, 0);
        switch (((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter)
        {
        case 0:
            tft.drawString("a", TFT_UI_START_X, TFT_UI_START_Y);
            break;
        case 1:
            tft.drawString("b", TFT_UI_START_X, TFT_UI_START_Y);
            break;
        default:
            break;
        }
        break;
    case 1:
        // tft.fillRect(TFT_UI_START_X + (2) * TFT_TEXT_WIDTH, TFT_UI_START_Y, 2 * TFT_TEXT_WIDTH, 20, 0);
        switch (((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter_2)
        {
        case 0:
            tft.fillRect(TFT_UI_START_X + (2) * TFT_TEXT_WIDTH, TFT_UI_START_Y, 2 * TFT_TEXT_WIDTH, 20, 0);
            tft.setTextSize(1);
            tft.drawString("o", TFT_UI_START_X + (2) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
            tft.setTextSize(TFT_TEXTSIZE);
            tft.drawString("C", TFT_UI_START_X + (3) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
            break;
        case 1:
            tft.fillRect(TFT_UI_START_X + (2) * TFT_TEXT_WIDTH, TFT_UI_START_Y, 2 * TFT_TEXT_WIDTH, 20, 0);
            tft.setTextSize(1);
            tft.drawString("o", TFT_UI_START_X + (2) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
            tft.setTextSize(TFT_TEXTSIZE);
            tft.drawString("F", TFT_UI_START_X + (3) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
            break;
        case 2:
            TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 1 * TFT_TEXT_WIDTH);
            TFT_menu_first_line_write((2) * TFT_TEXT_WIDTH, "LampRemain");
            break;
        case 3:
            TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 1 * TFT_TEXT_WIDTH);
            TFT_menu_first_line_write((2) * TFT_TEXT_WIDTH, "----");
            break;
        case 4:
            TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 1 * TFT_TEXT_WIDTH);
            TFT_menu_first_line_write((2) * TFT_TEXT_WIDTH, "TOC");
            break;
        case 5:
            TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 1 * TFT_TEXT_WIDTH);
            TFT_menu_first_line_write((2) * TFT_TEXT_WIDTH, "gC/L");
            break;
        case 6:
            TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 1 * TFT_TEXT_WIDTH);
            TFT_menu_first_line_write((2) * TFT_TEXT_WIDTH, "O-cm");
            break;
        case 7:
            TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 1 * TFT_TEXT_WIDTH);
            TFT_menu_first_line_write((2) * TFT_TEXT_WIDTH, "S/cm");
            break;
        case 8:
            TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 1 * TFT_TEXT_WIDTH);
            TFT_menu_first_line_write((2) * TFT_TEXT_WIDTH, "S/m");
            break;

        default:
            break;
        }

        break;
    case 2:
        tft.fillRect(TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);
        switch (((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter_3)
        {
        case 0:
            tft.drawString("Yes", TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH);
            break;
        case 1:
            tft.drawString("No", TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH);
            break;
        default:
            break;
        }
        break;
    case -1:
        TFT_menu_Arrow_write("<-");
        break;

    default:
        break;
    }
}

// Quick_parameter
void Quick_parameter_draw(int8_t Cursor)
{
    if (Cursor == 1)
    {
        TFT_menu_Arrow_write("<-");
    }
    else
    {

        // TFT_menu_first_line_write(0,FSM_State_Strings[g_FSMGlobal.Now_State]);
        tft.fillRect(TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);
        tft.drawString("TOC", TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH);
        // TFT_menu_Arrow_write("<-");
    }
}

void FSM_Quick_Setup_handler(void)
{
    static uint32_t last_press_time = 0;

    uint32_t current_time = HAL_GetTick();

    if ((current_time - last_press_time) >= 500)
    {
        switch (g_FSMGlobal.Quick_ui_Process)
        {
        case Quick_parameter:
            if (Loop_flag)
            {
                if (g_FSMGlobal.Quick_ui_Cursor == 1)
                {
                    TFT_menu_Arrow_write("<-");
                }
                else
                {

                    // TFT_menu_first_line_write(0,FSM_State_Strings[g_FSMGlobal.Now_State]);
                    tft.fillRect(TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);
                    tft.drawString("TOC", TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH);
                    // TFT_menu_Arrow_write("<-");
                }
                Loop_flag = false;
            }
            else
            {
                if (g_FSMGlobal.Quick_ui_Cursor == 1)
                {
                    TFT_menu_Arrow_Clear();
                }
                else
                {
                    tft.fillRect(TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);
                    tft.drawChar('_', TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, tft.fontcolor);
                }
                Loop_flag = true;
            }
            break;
        case Quick_Analog:
            if (Loop_flag)
            {
                Quick_Analog_draw(g_FSMGlobal.Quick_ui_Cursor);
                Loop_flag = false;
            }
            else
            {
                switch (g_FSMGlobal.Quick_ui_Cursor)
                {
                case 0:
                    tft.fillRect(TFT_UI_START_X, TFT_UI_START_Y, TFT_TEXT_WIDTH, 20, 0);
                    tft.drawString("_", TFT_UI_START_X, TFT_UI_START_Y);

                    break;
                case 1:
                    TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 10 * TFT_TEXT_WIDTH);

                    // tft.fillRect(TFT_UI_START_X + (2) * TFT_TEXT_WIDTH, TFT_UI_START_Y, 2 * TFT_TEXT_WIDTH, 20, 0);
                    tft.drawString("_", TFT_UI_START_X + (2) * TFT_TEXT_WIDTH, TFT_UI_START_Y);

                    break;
                case 2:
                    tft.fillRect(TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);
                    tft.drawString("_", TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH);
                    break;
                case 3:
                    TFT_menu_Arrow_Clear();
                    break;

                default:
                    break;
                }
                Loop_flag = true;
            }
            break;

        default:
            break;
        }

        last_press_time = current_time;
    }
}
void FSM_Configure_handler(void)
{
    static uint32_t last_press_time = 0;

    uint32_t current_time = HAL_GetTick();

    if ((current_time - last_press_time) >= 500)
    {
        if (Loop_flag)
        {
            TFT_menu_first_line_write(0, FSM_State_Strings[g_FSMGlobal.Now_State]);
            TFT_menu_second_line_write(0, FSM_State_Strings[g_FSMGlobal.Configure_ui_Process]);
            TFT_menu_Arrow_write("<-");
            Loop_flag = false;
        }
        else
        {
            TFT_menu_second_line_Clear(0, 265);
            Loop_flag = true;
        }

        last_press_time = current_time;
    }
}
void FSM_System_handler(void)
{
    static uint32_t last_press_time = 0;

    uint32_t current_time = HAL_GetTick();

    if ((current_time - last_press_time) >= 500)
    {
        if (Loop_flag)
        {
            TFT_menu_first_line_write(0, FSM_State_Strings[g_FSMGlobal.Now_State]);
            TFT_menu_second_line_write(0, FSM_State_Strings[g_FSMGlobal.System_ui_Process]);
            TFT_menu_Arrow_write("<-");
            Loop_flag = false;
        }
        else
        {
            TFT_menu_second_line_Clear(0, 265);
            Loop_flag = true;
        }

        last_press_time = current_time;
    }
}
void FSM_Service_handler(void)
{
    static uint32_t last_press_time = 0;

    uint32_t current_time = HAL_GetTick();

    if (current_time - last_press_time >= 500)
    {
        if (Loop_flag)
        {
            TFT_menu_first_line_write(0, FSM_State_Strings[g_FSMGlobal.Now_State]);
            TFT_menu_second_line_write(0, FSM_State_Strings[g_FSMGlobal.Service_ui_Process]);
            TFT_menu_Arrow_write("<-");
            Loop_flag = false;
        }
        else
        {
            TFT_menu_second_line_Clear(0, 265);
            Loop_flag = true;
        }

        last_press_time = current_time;
    }
}

void FSM_NONE_Event_LEFT_PRESS_UP_handler(void)
{
    // TFT_menu_ALL_Clear();
    // TFT_menu_first_line_write(0, "MENU");
    // TFT_menu_second_line_write(0, "Quick Setup");
    // TFT_menu_Arrow_write("<-");

    // g_FSMGlobal.Now_State = FSM_Menu;
    // g_UIManager.current_screen_id = FSM_Menu;
    // g_UIManager.current_screen = &g_UI_process_t[g_UIManager.current_screen_id];
    UI_change_run(FSM_Menu);
}

void FSM_Menu_Event_LEFT_PRESS_UP_handler(void)
{
}

void FSM_Cal_Event_LEFT_PRESS_UP_handler(void)
{

    if (--g_UIManager.current_screen->Cursor < -1)
    {
        g_UIManager.current_screen->Cursor = 0;
    }
}

void FSM_NONE_Event_RIGHT_PRESS_UP_handler(void)
{
    // TFT_menu_ALL_Clear();
    // TFT_menu_first_line_write(0, "Calibrate Analog");
    // TFT_menu_second_line_write(0, "Analog Output 1");
    // TFT_menu_Arrow_write("<-");
    // g_FSMGlobal.Cal_ui_Process = Cal_Analog_Pick;
    // g_FSMGlobal.Cal_ui_Parameter = 1;
    // g_FSMGlobal.Cal_ui_Cursor = 0;

    // g_FSMGlobal.Now_State = FSM_Cal;
    // g_UIManager.current_screen_id = FSM_Cal;
    // g_UIManager.current_screen = &g_UI_process_t[g_UIManager.current_screen_id];
    UI_change_run(FSM_Cal);
}

void FSM_Menu_Event_RIGHT_PRESS_UP_handler(void)
{
}

void FSM_Cal_Event_RIGHT_PRESS_UP_handler(void)
{
    if (++g_UIManager.current_screen->Cursor > 0)
    {
        g_UIManager.current_screen->Cursor = -1;
    }
}

void FSM_Event_ESC_handler(void)
{
    TFT_menu_first_line_Clear(0, 285);
    TFT_menu_second_line_Clear(0, 265);
    TFT_menu_Arrow_Clear();
}

void FSM_Menu_Event_UP_PRESS_UP_handler(void)
{
    if (++((UI_Menu_Parameter_t *)g_UIManager.current_screen->Parameter)->Menu_ui_Process > 3)
    {
        ((UI_Menu_Parameter_t *)g_UIManager.current_screen->Parameter)->Menu_ui_Process = 0;
    }
}

void FSM_Cal_Event_UP_PRESS_UP_handler(void)
{

    if (++((UI_Cal_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Aout_Num > 2)
    {
        ((UI_Cal_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Aout_Num = 1;
    }
}

void FSM_Menu_Event_DOWN_PRESS_UP_handler(void)
{
    if (--((UI_Menu_Parameter_t *)g_UIManager.current_screen->Parameter)->Menu_ui_Process < 0)
    {
        ((UI_Menu_Parameter_t *)g_UIManager.current_screen->Parameter)->Menu_ui_Process = 3;
    }
}

void FSM_Cal_Event_DOWN_PRESS_UP_handler(void)
{
    if (--((UI_Cal_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Aout_Num < 1)
    {
        ((UI_Cal_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Aout_Num = 2;
    }
}

void FSM_Menu_Event_ENTER_PRESS_UP_handler(void)
{
    switch (((UI_Menu_Parameter_t *)g_UIManager.current_screen->Parameter)->Menu_ui_Process)
    {
    case 0:

        UI_change_run(FSM_Quick_Setup);
        break;
    case 1:
        UI_change_run(FSM_Configure);
        break;
    case 2:

        UI_change_run(FSM_System);
        break;
    case 3:
        UI_change_run(FSM_Service);
        break;

    default:
        break;
    }

    Loop_flag = true;
}

// void FSM_NONE_struct_create(void)
// {
//     g_FSMGlobal.Now_State = FSM_NONE;
//     TFT_menu_ALL_Clear();
// }

void Cal_Analog_Pick_struct_create(void)
{
    g_FSMGlobal.Cal_ui_Process = Cal_Analog_Pick;
    g_FSMGlobal.Cal_ui_Cursor = 1;
    g_FSMGlobal.Cal_ui_Parameter = 1;
    Loop_flag = true;
    TFT_menu_ALL_Clear();
    TFT_menu_first_line_write(0, "Calibrate Analog");
    TFT_menu_second_line_write(0, "Analog Output 1");
    TFT_menu_Arrow_write("<-");
}

void Cal_4mA_struct_create(void)
{
    g_FSMGlobal.Cal_ui_Process = Cal_4mA;
    g_FSMGlobal.Cal_ui_Cursor = 4;
    if (g_FSMData_temp.Cal_Aout_Num == 1)
    {
        g_FSMGlobal.Cal_ui_Parameter = g_FSMData.Cal_Aout1_4mA;
    }
    else
    {
        g_FSMGlobal.Cal_ui_Parameter = g_FSMData.Cal_Aout2_4mA;
    }
    Loop_flag = true;
    TFT_menu_ALL_Clear();

    sprintf(str, "Aout%d 4mA Set %05d", g_FSMData_temp.Cal_Aout_Num, g_FSMGlobal.Cal_ui_Parameter);
    TFT_menu_first_line_write(0, str);
    TFT_menu_second_line_write(0, "Press ENTER when Done");
    TFT_menu_Arrow_write("<-");
}

void Cal_20mA_struct_create(void)
{
    if (g_FSMData_temp.Cal_Aout_Num == 1)
    {
        g_FSMData_temp.Cal_Aout1_4mA = g_FSMGlobal.Cal_ui_Parameter;
        g_FSMGlobal.Cal_ui_Parameter = g_FSMData.Cal_Aout1_20mA;
    }
    else
    {
        g_FSMData_temp.Cal_Aout2_4mA = g_FSMGlobal.Cal_ui_Parameter;
        g_FSMGlobal.Cal_ui_Parameter = g_FSMData.Cal_Aout2_20mA;
    }
    g_FSMGlobal.Cal_ui_Process = Cal_20mA;
    g_FSMGlobal.Cal_ui_Cursor = 4;
    Loop_flag = true;
    TFT_menu_ALL_Clear();

    sprintf(str, "Aout%d 20mA Set %05d", g_FSMData_temp.Cal_Aout_Num, g_FSMGlobal.Cal_ui_Parameter);
    TFT_menu_first_line_write(0, str);
    TFT_menu_second_line_write(0, "Press ENTER when Done");
    TFT_menu_Arrow_write("<-");
}

void Cal_Save_Enable_struct_create(void)
{
    if (g_FSMData_temp.Cal_Aout_Num == 1)
    {
        g_FSMData_temp.Cal_Aout1_20mA = g_FSMGlobal.Cal_ui_Parameter;
    }
    else
    {
        g_FSMData_temp.Cal_Aout2_20mA = g_FSMGlobal.Cal_ui_Parameter;
    }
    g_FSMGlobal.Cal_ui_Process = Cal_Save_Enable;
    g_FSMGlobal.Cal_ui_Cursor = 0;
    g_FSMGlobal.Cal_ui_Parameter = 0;
    Loop_flag = true;
    TFT_menu_ALL_Clear();

    TFT_menu_first_line_write(0, "Save Calibration Yes");
    TFT_menu_second_line_write(0, "Press ENTER to Exit");
    TFT_menu_Arrow_write("<-");
}

void Cal_Successful_struct_create(void)
{
    g_FSMGlobal.Cal_ui_Process = Cal_Successful;
    g_FSMGlobal.Cal_ui_Cursor = 0;

    Loop_flag = true;
    TFT_menu_ALL_Clear();

    TFT_menu_first_line_write(0, "Calibration Successful");
    TFT_menu_second_line_write(0, "Press ENTER to Continue");
}

void FSM_Cal_Event_ENTER_PRESS_UP_handler(void)
{
    if (g_UIManager.current_screen->Cursor == -1)
    {
        UI_change_run(FSM_NONE);
    }
    else
    {
        UI_change_run(FSM_Cal_2);
    }
}

void FSM_Quick_Setup_Event_UP_PRESS_UP_handler(void)
{
    switch (g_FSMGlobal.Quick_ui_Process)
    {
    case Quick_Analog:
        switch (g_FSMGlobal.Quick_ui_Cursor)
        {
        case 0:
            g_FSMGlobal.Quick_ui_Parameter++;
            if (g_FSMGlobal.Quick_ui_Parameter > 1)
            {
                g_FSMGlobal.Quick_ui_Parameter = 0;
            }
            break;
        case 1:
            g_FSMGlobal.Quick_ui_Parameter2++;
            if (g_FSMGlobal.Quick_ui_Parameter2 > 8)
            {
                g_FSMGlobal.Quick_ui_Parameter2 = 0;
            }
            break;
        case 2:
            g_FSMGlobal.Quick_ui_Parameter3++;
            if (g_FSMGlobal.Quick_ui_Parameter3 > 1)
            {
                g_FSMGlobal.Quick_ui_Parameter3 = 0;
            }
            break;
        default:
            break;
        }

        switch (g_FSMGlobal.Quick_ui_Cursor)
        {
        case 0:
            tft.fillRect(TFT_UI_START_X, TFT_UI_START_Y, TFT_TEXT_WIDTH, 20, 0);

            break;
        case 1:
            TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 10 * TFT_TEXT_WIDTH);

            break;
        case 2:
            tft.fillRect(TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);

            break;

        default:
            break;
        }
        Quick_Analog_draw(g_FSMGlobal.Quick_ui_Cursor);
        Loop_flag = true;
        break;

    default:
        break;
    }
}
void FSM_Quick_Setup_Event_DOWN_PRESS_UP_handler(void)
{
    switch (g_FSMGlobal.Quick_ui_Process)
    {
    case Quick_Analog:
        switch (g_FSMGlobal.Quick_ui_Cursor)
        {
        case 0:
            g_FSMGlobal.Quick_ui_Parameter--;
            if (g_FSMGlobal.Quick_ui_Parameter < 0)
            {
                g_FSMGlobal.Quick_ui_Parameter = 1;
            }
            break;
        case 1:
            g_FSMGlobal.Quick_ui_Parameter2--;
            if (g_FSMGlobal.Quick_ui_Parameter2 < 0)
            {
                g_FSMGlobal.Quick_ui_Parameter2 = 8;
            }
            break;
        case 2:
            g_FSMGlobal.Quick_ui_Parameter3--;
            if (g_FSMGlobal.Quick_ui_Parameter3 < 0)
            {
                g_FSMGlobal.Quick_ui_Parameter3 = 1;
            }
            break;

        default:
            break;
        }

        switch (g_FSMGlobal.Quick_ui_Cursor)
        {
        case 0:
            tft.fillRect(TFT_UI_START_X, TFT_UI_START_Y, TFT_TEXT_WIDTH, 20, 0);

            break;
        case 1:
            TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 10 * TFT_TEXT_WIDTH);

            break;
        case 2:
            tft.fillRect(TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);

            break;

        default:
            break;
        }
        Quick_Analog_draw(g_FSMGlobal.Quick_ui_Cursor);
        Loop_flag = true;
        break;
    default:
        break;
    }
}

void Celsius_display(uint16_t x, uint8_t line)
{
    tft.setTextSize(1);
    tft.drawString("o", TFT_UI_START_X + x, TFT_UI_START_Y + (line - 1) * TFT_TEXT_LENGTH);
    tft.setTextSize(TFT_TEXTSIZE);
    tft.drawString("C", TFT_UI_START_X + x + TFT_TEXT_WIDTH, TFT_UI_START_Y + (line - 1) * TFT_TEXT_LENGTH);
}

void FSM_Quick_Setup_Event_ENTER_PRESS_UP_handler(void)
{
    if (g_UIManager.current_screen->Cursor == -1)
    {
        UI_change_run(FSM_NONE);
    }
    else
    {
        UI_change_run(FSM_Quick_Setup_2);
    }
}
void FSM_Quick_Setup_Event_RIGHT_PRESS_UP_handler(void)
{
    switch (g_FSMGlobal.Quick_ui_Process)
    {
    case Quick_parameter:
        Loop_flag = false;
        Quick_parameter_draw(g_FSMGlobal.Quick_ui_Cursor);
        g_FSMGlobal.Quick_ui_Cursor++;
        if (g_FSMGlobal.Quick_ui_Cursor > 1)
        {
            g_FSMGlobal.Quick_ui_Cursor = 0;
        }
        break;
    case Quick_Analog:
        Loop_flag = false;
        Quick_Analog_draw(g_FSMGlobal.Quick_ui_Cursor);

        g_FSMGlobal.Quick_ui_Cursor++;
        if (g_FSMGlobal.Quick_ui_Cursor > 3)
        {
            g_FSMGlobal.Quick_ui_Cursor = 0;
        }

        break;

    default:
        break;
    }
}
void FSM_Quick_Setup_Event_LEFT_PRESS_UP_handler(void)
{
    switch (g_FSMGlobal.Quick_ui_Process)
    {
    case Quick_parameter:
        Loop_flag = false;
        Quick_parameter_draw(g_FSMGlobal.Quick_ui_Cursor);
        g_FSMGlobal.Quick_ui_Cursor--;
        if (g_FSMGlobal.Quick_ui_Cursor < 0)
        {
            g_FSMGlobal.Quick_ui_Cursor = 1;
        }
        break;
    case Quick_Analog:
        Loop_flag = false;
        Quick_Analog_draw(g_FSMGlobal.Quick_ui_Cursor);
        g_FSMGlobal.Quick_ui_Cursor--;
        if (g_FSMGlobal.Quick_ui_Cursor < 0)
        {
            g_FSMGlobal.Quick_ui_Cursor = 3;
        }

        break;

    default:
        break;
    }
}

void FSM_Quick_Setup_2_Event_UP_PRESS_UP_handler(void)
{
    switch (g_UIManager.current_screen->Cursor)
    {
    case 0:
        if (++((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter > 1)
        {
            ((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter = 0;
        }
        break;
    case 1:
        if (++((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter_2 > 8)
        {
            ((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter_2 = 0;
        }
        break;
    case 2:
        if (++((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter_3 > 1)
        {
            ((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter_3 = 0;
        }
        break;
    default:
        break;
    }

    switch (g_UIManager.current_screen->Cursor)
    {
    case 0:
        tft.fillRect(TFT_UI_START_X, TFT_UI_START_Y, TFT_TEXT_WIDTH, 20, 0);

        break;
    case 1:
        TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 10 * TFT_TEXT_WIDTH);

        break;
    case 2:
        tft.fillRect(TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);

        break;

    default:
        break;
    }
    Quick_Analog_draw(g_UIManager.current_screen->Cursor);
    Loop_flag = true;
}

void FSM_Quick_Setup_2_Event_DOWN_PRESS_UP_handler(void)
{
    switch (g_UIManager.current_screen->Cursor)
    {
    case 0:
        if (--((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter < 0)
        {
            ((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter = 1;
        }
        break;
    case 1:
        if (--((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter_2 < 0)
        {
            ((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter_2 = 8;
        }
        break;
    case 2:
        if (--((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter_3 < 0)
        {
            ((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter_3 = 1;
        }
        break;
    default:
        break;
    }

    switch (g_UIManager.current_screen->Cursor)
    {
    case 0:
        tft.fillRect(TFT_UI_START_X, TFT_UI_START_Y, TFT_TEXT_WIDTH, 20, 0);

        break;
    case 1:
        TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 10 * TFT_TEXT_WIDTH);

        break;
    case 2:
        tft.fillRect(TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);

        break;

    default:
        break;
    }
    Quick_Analog_draw(g_UIManager.current_screen->Cursor);
    Loop_flag = true;
}
void FSM_Quick_Setup_2_Event_LEFT_PRESS_UP_handler(void)
{
    Loop_flag = false;
    Quick_Analog_draw(g_UIManager.current_screen->Cursor);
    if (--g_UIManager.current_screen->Cursor < -1)
    {
        g_UIManager.current_screen->Cursor = 2;
    }
}
void FSM_Quick_Setup_2_Event_RIGHT_PRESS_UP_handler(void)
{
    Loop_flag = false;
    Quick_Analog_draw(g_UIManager.current_screen->Cursor);

    if (++g_UIManager.current_screen->Cursor > 2)
    {
        g_UIManager.current_screen->Cursor = -1;
    }
}
void FSM_Quick_Setup_2_Event_ENTER_PRESS_UP_handler(void)
{
    if (g_UIManager.current_screen->Cursor == -1)
    {
        UI_change_run(FSM_Quick_Setup);
    }else
    {
        UI_change_run(FSM_Quick_Setup_3);
    }
}

void FSM_Quick_Setup_3_Event_UP_PRESS_UP_handler(void)
{

}

void FSM_Quick_Setup_3_Event_DOWN_PRESS_UP_handler(void)
{

}
void FSM_Quick_Setup_3_Event_LEFT_PRESS_UP_handler(void)
{

}
void FSM_Quick_Setup_3_Event_RIGHT_PRESS_UP_handler(void)
{

}
void FSM_Quick_Setup_3_Event_ENTER_PRESS_UP_handler(void)
{

    //UI_change_run(FSM_Quick_Setup_2);
}



void FSM_Configure_Event_UP_PRESS_UP_handler(void)
{

    if (++((UI_Configure_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Configure_Parameter > 7)
    {
        ((UI_Configure_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Configure_Parameter = 0;
    }
}
void FSM_Configure_Event_DOWN_PRESS_UP_handler(void)
{
    if (--((UI_Configure_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Configure_Parameter < 0)
    {
        ((UI_Configure_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Configure_Parameter = 7;
    }
}
// static inline void FSM_Measurement_Func()
//{
// }

void FSM_Configure_Event_ENTER_PRESS_UP_handler(void)
{

    switch (((UI_Configure_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Configure_Parameter)
    {
    case 0:
        UI_change_run(FSM_Measurement);
        break;
    case 1:
        UI_change_run(FSM_Analog_Outputs);
        break;
    case 2:
        UI_change_run(FSM_Set_Points);
        break;
    case 3:
        UI_change_run(FSM_Alarm);
        break;
    case 4:
        UI_change_run(FSM_TOC_Setup);
        break;
    case 5:
        UI_change_run(FSM_Display);
        break;
    case 6:
        UI_change_run(FSM_Flow_Control);
        break;
    case 7:
        UI_change_run(FSM_Hold_outputs);
        break;
    default:
        break;
    }

    Loop_flag = true;
}
void FSM_Configure_Event_RIGHT_PRESS_UP_handler(void)
{
}
void FSM_Configure_Event_LEFT_PRESS_UP_handler(void)
{
}

void FSM_System_Event_UP_PRESS_UP_handler(void)
{
}
void FSM_System_Event_DOWN_PRESS_UP_handler(void)
{
}
void FSM_System_Event_ENTER_PRESS_UP_handler(void)
{
}
void FSM_System_Event_RIGHT_PRESS_UP_handler(void)
{
}
void FSM_System_Event_LEFT_PRESS_UP_handler(void)
{
}
void FSM_Service_Event_UP_PRESS_UP_handler(void)
{
}
void FSM_Service_Event_DOWN_PRESS_UP_handler(void)
{
}
void FSM_Service_Event_ENTER_PRESS_UP_handler(void)
{
}
void FSM_Service_Event_RIGHT_PRESS_UP_handler(void)
{
}
void FSM_Service_Event_LEFT_PRESS_UP_handler(void)
{
}

void FSM_Measurement_Event_UP_PRESS_UP_handler(void)
{
    g_FSMGlobal.Measurement_ui_Process++;
    if (g_FSMGlobal.Measurement_ui_Process > Mea_Set_Averaging)
    {
        g_FSMGlobal.Measurement_ui_Process = Mea_Channal_Setup;
    }

    TFT_menu_second_line_Clear(0, 265);
    switch (g_FSMGlobal.Measurement_ui_Process)
    {
    case Mea_Channal_Setup:
        TFT_menu_second_line_write(0, "Channel Setup");
        break;
    case Mea_Resistivity:
        TFT_menu_second_line_write(0, "Resistivity");
        break;
    case Mea_Set_Averaging:
        TFT_menu_second_line_write(0, "Set Averaging");
        break;
    default:
        break;
    }
}
void FSM_Measurement_Event_DOWN_PRESS_UP_handler(void)
{
    if (g_FSMGlobal.Measurement_ui_Process > 0)
    {
        g_FSMGlobal.Measurement_ui_Process--;
    }
    else
    {
        g_FSMGlobal.Measurement_ui_Process = Mea_Set_Averaging;
    }
    TFT_menu_second_line_Clear(0, 265);
    switch (g_FSMGlobal.Measurement_ui_Process)
    {
    case Mea_Channal_Setup:
        TFT_menu_second_line_write(0, "Channel Setup");
        break;
    case Mea_Resistivity:
        TFT_menu_second_line_write(0, "Resistivity");
        break;
    case Mea_Set_Averaging:
        TFT_menu_second_line_write(0, "Set Averaging");
        break;
    default:
        break;
    }
}
void FSM_Measurement_Event_ENTER_PRESS_UP_handler(void)
{
    switch (g_FSMGlobal.Measurement_ui_Process)
    {
    case Mea_Channal_Setup:
        g_FSMGlobal.Now_State = FSM_Channel_Setup;
        g_FSMGlobal.Channel_Setup_ui_Process = Channel_Setup_parameter;
        g_FSMGlobal.Channel_Setup_ui_Cursor = 0;
        g_FSMGlobal.Channel_Setup_ui_Parameter = 0;
        g_FSMGlobal.Channel_Setup_ui_Parameter2 = 0;
        g_FSMGlobal.Channel_Setup_ui_Parameter3 = 0;
        g_FSMGlobal.Channel_Setup_ui_Parameter4 = 0;

        TFT_menu_ALL_Clear();
        // TFT_menu_first_line_write(0,  "aA            (      )");
        // TFT_menu_second_line_write(0, "bA  C         (      )");
        // TFT_menu_Arrow_write("<-");

        TFT_menu_second_line_write(0, "Parameter = TOC");
        TFT_menu_Arrow_write("<-");
        break;
    case Mea_Resistivity:
        g_FSMGlobal.Now_State = FSM_Resistivity;
        g_FSMGlobal.Resistivity_ui_Process = Resistivity_compensation;

        TFT_menu_ALL_Clear();
        TFT_menu_first_line_write(0, "a Compensation=Standard");
        TFT_menu_second_line_write(0, "b Compensation=Standard");
        TFT_menu_Arrow_write("<-");
        break;
    case Mea_Set_Averaging:
        g_FSMGlobal.Now_State = FSM_Set_Averaging;
        g_FSMGlobal.Set_Averaging_ui_Process = Set_Averaging_Setup;

        TFT_menu_ALL_Clear();
        TFT_menu_first_line_write(0, "a Average = None");
        TFT_menu_second_line_write(0, "b Average = High");
        TFT_menu_Arrow_write("<-");
        break;
    default:
        break;
    }
}
void FSM_Measurement_Event_RIGHT_PRESS_UP_handler(void)
{
}
void FSM_Measurement_Event_LEFT_PRESS_UP_handler(void)
{
}

void FSM_Measurement_handler(void)
{
    static uint32_t last_press_time = 0;

    uint32_t current_time = HAL_GetTick();

    if (current_time - last_press_time >= 500)
    {
        if (Loop_flag)
        {
            switch (g_FSMGlobal.Measurement_ui_Process)
            {
            case Mea_Channal_Setup:
                TFT_menu_second_line_write(0, "Channel Setup");
                break;
            case Mea_Resistivity:
                TFT_menu_second_line_write(0, "Resistivity");
                break;
            case Mea_Set_Averaging:
                TFT_menu_second_line_write(0, "Set Averaging");
                break;
            default:
                break;
            }
            TFT_menu_Arrow_write("<-");
            Loop_flag = false;
        }
        else
        {
            TFT_menu_second_line_Clear(0, 265);
            Loop_flag = true;
        }

        last_press_time = current_time;
    }
}

void FSM_Channel_Setup_handler(void)
{
    static uint32_t last_press_time = 0;

    uint32_t current_time = HAL_GetTick();

    if (current_time - last_press_time >= 500)
    {
        switch (g_FSMGlobal.Channel_Setup_ui_Process)
        {
        case Channel_Setup_parameter:

            if (Loop_flag)
            {
                // TFT_menu_first_line_write(0,FSM_State_Strings[g_FSMGlobal.Now_State]);
                tft.fillRect(TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);
                tft.drawString("TOC", TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH);
                TFT_menu_Arrow_write("<-");
                Loop_flag = false;
            }
            else
            {
                tft.fillRect(TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);
                tft.drawChar('_', TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, tft.fontcolor);
                Loop_flag = true;
            }
            break;
        default:
            break;
        }

        last_press_time = current_time;
    }
}
void FSM_Resistivity_handler(void)
{
}
void FSM_Set_Averaging_handler(void)
{
}

void FSM_Channel_Setup_Event_UP_PRESS_UP_handler(void)
{
}
void FSM_Channel_Setup_Event_DOWN_PRESS_UP_handler(void)
{
}

static inline void FSM_Channel_Setup_parameter(char abcd, int measurements, int multiplier)
{
    switch (abcd)
    {
    case 'a':
        TFT_menu_first_line_write(0, "a");
        break;
    case 'b':
        TFT_menu_second_line_write(0, "b");
        break;
    case 'c':
        TFT_menu_first_line_write(0, "c");
        break;
    case 'd':
        TFT_menu_second_line_write(0, "d");
        break;
    default:
        break;
    }

    if (abcd == 'a' || abcd == 'c')
    {
        TFT_menu_first_line_write(2 * TFT_TEXT_WIDTH, TOC_measurements_Strings[measurements]);
        TFT_menu_first_line_write(14 * TFT_TEXT_WIDTH, "(     )");
        switch (measurements)
        {
        case meas_toc:
            switch (multiplier)
            {
            case 0:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "ppb");
                break;
            case 1:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "ppt");
                break;
            case 2:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Auto");
                break;
            case 3:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "ppm");
                break;
            default:
                break;
            }

            break;
        case meas_gc_L:
            switch (multiplier)
            {
            case 0:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Micro");
                break;
            case 1:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Nano");
                break;
            case 2:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Auto");
                break;
            case 3:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Milli");
                break;
            default:
                break;
            }

            break;
        case meas_O_cm:
            switch (multiplier)
            {
            case 0:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Auto");
                break;
            case 1:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Mega");
                break;
            case 2:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Kilo");
                break;
            case 3:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Units");
                break;
            default:
                break;
            }
            break;
        case meas_S_m:
            switch (multiplier)
            {
            case 0:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Auto");
                break;
            case 1:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Units");
                break;
            case 2:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Micro");
                break;
            case 3:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Milli");
                break;
            default:
                break;
            }
            break;
        case meas_S_cm:
            switch (multiplier)
            {
            case 0:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Auto");
                break;
            case 1:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Units");
                break;
            case 2:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Micro");
                break;
            case 3:
                TFT_menu_first_line_write(15 * TFT_TEXT_WIDTH, "Milli");
                break;
            default:
                break;
            }
            break;

        default:
            break;
        }
    }
    else if (abcd == 'b' || abcd == 'd')
    {
        TFT_menu_second_line_write(2 * TFT_TEXT_WIDTH, TOC_measurements_Strings[measurements]);
        TFT_menu_second_line_write(14 * TFT_TEXT_WIDTH, "(     )");
        switch (measurements)
        {
        case meas_toc:
            switch (multiplier)
            {
            case 0:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "ppb");
                break;
            case 1:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "ppt");
                break;
            case 2:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Auto");
                break;
            case 3:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "ppm");
                break;
            default:
                break;
            }

            break;
        case meas_gc_L:
            switch (multiplier)
            {
            case 0:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Micro");
                break;
            case 1:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Nano");
                break;
            case 2:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Auto");
                break;
            case 3:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Milli");
                break;
            default:
                break;
            }

            break;
        case meas_O_cm:
            switch (multiplier)
            {
            case 0:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Auto");
                break;
            case 1:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Mega");
                break;
            case 2:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Kilo");
                break;
            case 3:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Units");
                break;
            default:
                break;
            }
            break;
        case meas_S_m:
            switch (multiplier)
            {
            case 0:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Auto");
                break;
            case 1:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Units");
                break;
            case 2:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Micro");
                break;
            case 3:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Milli");
                break;
            default:
                break;
            }
            break;
        case meas_S_cm:
            switch (multiplier)
            {
            case 0:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Auto");
                break;
            case 1:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Units");
                break;
            case 2:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Micro");
                break;
            case 3:
                TFT_menu_second_line_write(15 * TFT_TEXT_WIDTH, "Milli");
                break;
            default:
                break;
            }
            break;

        default:
            break;
        }
    }
}

void FSM_Channel_Setup_Event_ENTER_PRESS_UP_handler(void)
{
    switch (g_FSMGlobal.Channel_Setup_ui_Process)
    {
    case Channel_Setup_parameter:
        g_FSMGlobal.Channel_Setup_ui_Process = Channel_Setup_a_b_Unit;
        g_FSMGlobal.Channel_Setup_ui_Cursor = 0;
        g_FSMGlobal.Channel_Setup_ui_Parameter = g_FSMData.Channel_Setup_a_measurements;
        g_FSMGlobal.Channel_Setup_ui_Parameter2 = g_FSMData.Channel_Setup_a_unit_multiplier;
        g_FSMGlobal.Channel_Setup_ui_Parameter3 = g_FSMData.Channel_Setup_b_measurements;
        g_FSMGlobal.Channel_Setup_ui_Parameter4 = g_FSMData.Channel_Setup_b_unit_multiplier;
        TFT_menu_ALL_Clear();
        // TFT_menu_first_line_write(0, "aA            (      )");

        FSM_Channel_Setup_parameter('a', g_FSMGlobal.Channel_Setup_ui_Parameter, g_FSMGlobal.Channel_Setup_ui_Parameter2);
        FSM_Channel_Setup_parameter('b', g_FSMGlobal.Channel_Setup_ui_Parameter3, g_FSMGlobal.Channel_Setup_ui_Parameter4);
        // TFT_menu_second_line_write(0, "bA  C         (      )");
        TFT_menu_Arrow_write("<-");
        break;
    default:
        break;
    }
}
void FSM_Channel_Setup_Event_RIGHT_PRESS_UP_handler(void)
{
}
void FSM_Channel_Setup_Event_LEFT_PRESS_UP_handler(void)
{
}

void FSM_Resistivity_Event_UP_PRESS_UP_handler(void)
{
}
void FSM_Resistivity_Event_DOWN_PRESS_UP_handler(void)
{
}
void FSM_Resistivity_Event_ENTER_PRESS_UP_handler(void)
{
}
void FSM_Resistivity_Event_RIGHT_PRESS_UP_handler(void)
{
}
void FSM_Resistivity_Event_LEFT_PRESS_UP_handler(void)
{
}

void FSM_Set_Averaging_Event_UP_PRESS_UP_handler(void)
{
}
void FSM_Set_Averaging_Event_DOWN_PRESS_UP_handler(void)
{
}
void FSM_Set_Averaging_Event_ENTER_PRESS_UP_handler(void)
{
}
void FSM_Set_Averaging_Event_RIGHT_PRESS_UP_handler(void)
{
}
void FSM_Set_Averaging_Event_LEFT_PRESS_UP_handler(void)
{
}

// 校准成功处理函数
void FSM_Cal_Successful_handler(void)
{
    g_FSMData.Cal_Aout1_4mA = g_FSMData_temp.Cal_Aout1_4mA;
    g_FSMData.Cal_Aout1_20mA = g_FSMData_temp.Cal_Aout1_20mA;
    g_FSMData.Cal_Aout2_4mA = g_FSMData_temp.Cal_Aout2_4mA;
    g_FSMData.Cal_Aout2_20mA = g_FSMData_temp.Cal_Aout2_20mA;
}

void FSM_Cal_2_Event_UP_PRESS_UP_handler(void)
{
    int Parameter_Buff = power_of_10[4 - g_UIManager.current_screen->Cursor];
    if (g_UIManager.current_screen->Cursor == -1)
    {
    }
    else
    {
        ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter += Parameter_Buff;
        if (((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter > 50000)
        {
            ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter = 0;
        }

        Loop_flag = true;
        char buffer[6];
        sprintf(buffer, "%05d", ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter);
        tft.fillRect(TFT_UI_START_X + (14) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH * 5, TFT_TEXT_LENGTH, 0);
        tft.drawString(buffer, TFT_UI_START_X + (14) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
    }
}

void FSM_Cal_2_Event_DOWN_PRESS_UP_handler(void)
{

    int Parameter_Buff = power_of_10[4 - g_UIManager.current_screen->Cursor];
    if (g_UIManager.current_screen->Cursor == -1)
    {
    }
    else
    {
        ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter -= Parameter_Buff;
        if (((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter < 0)
        {
            ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter = 0;
        }

        Loop_flag = true;
        char buffer[6];
        sprintf(buffer, "%05d", ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter);
        tft.fillRect(TFT_UI_START_X + (14) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH * 5, TFT_TEXT_LENGTH, 0);
        tft.drawString(buffer, TFT_UI_START_X + (14) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
    }
}

void FSM_Cal_2_Event_LEFT_PRESS_UP_handler(void)
{
    if (g_UIManager.current_screen->Cursor == -1)
    {
        TFT_menu_Arrow_write("<-");
    }
    else
    {
        int temp = ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter / power_of_10[4 - g_UIManager.current_screen->Cursor] % 10;

        tft.fillRect(TFT_UI_START_X + (14 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
        tft.drawChar(temp + '0', TFT_UI_START_X + (14 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
    }

    if (--g_UIManager.current_screen->Cursor < -1)
    {
        g_UIManager.current_screen->Cursor = 4;
    }

    if (g_UIManager.current_screen->Cursor != -1)
    {
        Loop_flag = false;
        tft.fillRect(TFT_UI_START_X + (14 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
        tft.drawChar('_', TFT_UI_START_X + (14 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
    }
}

void FSM_Cal_2_Event_RIGHT_PRESS_UP_handler(void)
{

    if (g_UIManager.current_screen->Cursor == -1)
    {
        TFT_menu_Arrow_write("<-");
    }
    else
    {
        int temp = ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter / power_of_10[4 - g_UIManager.current_screen->Cursor] % 10;

        tft.fillRect(TFT_UI_START_X + (14 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
        tft.drawChar(temp + '0', TFT_UI_START_X + (14 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
    }

    if (++g_UIManager.current_screen->Cursor > 4)
    {
        g_UIManager.current_screen->Cursor = -1;
    }

    if (g_UIManager.current_screen->Cursor != -1)
    {
        Loop_flag = false;
        tft.fillRect(TFT_UI_START_X + (14 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
        tft.drawChar('_', TFT_UI_START_X + (14 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
    }
}

void FSM_Cal_2_Event_ENTER_PRESS_UP_handler(void)
{
    if (g_UIManager.current_screen->Cursor == -1)
    {
        UI_change_run(FSM_Cal);
    }
    else
    {
        UI_change_run(FSM_Cal_3);
    }
}

void FSM_Cal_3_Event_UP_PRESS_UP_handler(void)
{
    int Parameter_Buff = power_of_10[4 - g_UIManager.current_screen->Cursor];
    if (g_UIManager.current_screen->Cursor == -1)
    {
    }
    else
    {
        ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter += Parameter_Buff;
        if (((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter > 50000)
        {
            ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter = 0;
        }

        Loop_flag = true;
        char buffer[6];
        sprintf(buffer, "%05d", ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter);
        tft.fillRect(TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH * 5, TFT_TEXT_LENGTH, 0);
        tft.drawString(buffer, TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
    }
}

void FSM_Cal_3_Event_DOWN_PRESS_UP_handler(void)
{
    int Parameter_Buff = power_of_10[4 - g_UIManager.current_screen->Cursor];
    if (g_UIManager.current_screen->Cursor == -1)
    {
    }
    else
    {
        ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter -= Parameter_Buff;
        if (((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter < 0)
        {
            ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter = 0;
        }

        Loop_flag = true;
        char buffer[6];
        sprintf(buffer, "%05d", ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter);
        tft.fillRect(TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH * 5, TFT_TEXT_LENGTH, 0);
        tft.drawString(buffer, TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
    }
}

void FSM_Cal_3_Event_LEFT_PRESS_UP_handler(void)
{
    if (g_UIManager.current_screen->Cursor == -1)
    {
        TFT_menu_Arrow_write("<-");
    }
    else
    {
        int temp = ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter / power_of_10[4 - g_UIManager.current_screen->Cursor] % 10;

        tft.fillRect(TFT_UI_START_X + (15 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
        tft.drawChar(temp + '0', TFT_UI_START_X + (15 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
    }

    if (--g_UIManager.current_screen->Cursor < -1)
    {
        g_UIManager.current_screen->Cursor = 4;
    }

    if (g_UIManager.current_screen->Cursor != -1)
    {
        Loop_flag = false;
        tft.fillRect(TFT_UI_START_X + (15 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
        tft.drawChar('_', TFT_UI_START_X + (15 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
    }
}

void FSM_Cal_3_Event_RIGHT_PRESS_UP_handler(void)
{

    if (g_UIManager.current_screen->Cursor == -1)
    {
        TFT_menu_Arrow_write("<-");
    }
    else
    {
        int temp = ((UI_Cal_2_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter / power_of_10[4 - g_UIManager.current_screen->Cursor] % 10;

        tft.fillRect(TFT_UI_START_X + (15 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
        tft.drawChar(temp + '0', TFT_UI_START_X + (15 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
    }

    if (++g_UIManager.current_screen->Cursor > 4)
    {
        g_UIManager.current_screen->Cursor = -1;
    }

    if (g_UIManager.current_screen->Cursor != -1)
    {
        Loop_flag = false;
        tft.fillRect(TFT_UI_START_X + (15 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
        tft.drawChar('_', TFT_UI_START_X + (15 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
    }
}
void FSM_Cal_3_Event_ENTER_PRESS_UP_handler(void)
{
    if (g_UIManager.current_screen->Cursor == -1)
    {
        UI_change_run(FSM_Cal_2);
    }
    else
    {
        UI_change_run(FSM_Cal_4);
    }
}

void FSM_Cal_4_Event_UP_PRESS_UP_handler(void)
{
    if (g_UIManager.current_screen->Cursor == -1)
    {
    }
    else
    {

        if (++((UI_Cal_4_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter > 1)
        {
            ((UI_Cal_4_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter = 0;
        }

        Loop_flag = true;
        tft.fillRect(TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH * 3, TFT_TEXT_LENGTH, 0);
        if (((UI_Cal_4_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter == 0)
        {
            tft.drawString("Yes", TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
        }
        else
        {
            tft.drawString("No", TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
        }
    }
}
void FSM_Cal_4_Event_DOWN_PRESS_UP_handler(void)
{
    if (g_UIManager.current_screen->Cursor == -1)
    {
    }
    else
    {
        if (--((UI_Cal_4_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter < 0)
        {
            ((UI_Cal_4_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter = 1;
        }

        Loop_flag = true;
        tft.fillRect(TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH * 3, TFT_TEXT_LENGTH, 0);
        if (((UI_Cal_4_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter == 0)
        {
            tft.drawString("Yes", TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
        }
        else
        {
            tft.drawString("No", TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
        }
    }
}
void FSM_Cal_4_Event_LEFT_PRESS_UP_handler(void)
{
    if (g_UIManager.current_screen->Cursor == -1)
    {
        TFT_menu_Arrow_write("<-");
    }
    else
    {

        tft.fillRect(TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
        if (((UI_Cal_4_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter == 0)
        {
            tft.drawString("Yes", TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
        }
        else
        {
            tft.drawString("No", TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
        }
    }

    if (--g_UIManager.current_screen->Cursor < -1)
    {
        g_UIManager.current_screen->Cursor = 0;
    }
    Loop_flag = true;
}
void FSM_Cal_4_Event_RIGHT_PRESS_UP_handler(void)
{
    if (g_UIManager.current_screen->Cursor == -1)
    {
        TFT_menu_Arrow_write("<-");
    }
    else
    {

        tft.fillRect(TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
        if (((UI_Cal_4_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter == 0)
        {
            tft.drawString("Yes", TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
        }
        else
        {
            tft.drawString("No", TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
        }
    }

    if (++g_UIManager.current_screen->Cursor > 0)
    {
        g_UIManager.current_screen->Cursor = -1;
    }
    Loop_flag = true;
}
void FSM_Cal_4_Event_ENTER_PRESS_UP_handler(void)
{

    if (g_UIManager.current_screen->Cursor == -1)
    {
        UI_change_run(FSM_Cal_3);
    }
    else
    {
        if (((UI_Cal_4_Parameter_t *)(g_UIManager.current_screen->Parameter))->UI_Cal_Parameter == 0)
            UI_change_run(FSM_Cal_5);
        else
            UI_change_run(FSM_NONE);
    }
}

void FSM_Cal_5_Event_ENTER_PRESS_UP_handler(void)
{
    FSM_Cal_Successful_handler();
    UI_change_run(FSM_NONE);
}

/// @brief ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FSM_NONE_struct_create(void)
{
    TFT_menu_ALL_Clear();
}

void FSM_NONE_handle_state_while(void)
{
}

void FSM_NONE_handle_event(FSM_Event_t event)
{
    switch (event)
    {
    case FSM_Event_LEFT_PRESS_UP:
        FSM_NONE_Event_LEFT_PRESS_UP_handler();
        break;
    case FSM_Event_RIGHT_PRESS_UP:
        FSM_NONE_Event_RIGHT_PRESS_UP_handler();
        break;
    default:
        break;
    }
}

void FSM_NONE_struct_destroy(void)
{
}

void FSM_Menu_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_first_line_write(0, "MENU");
    TFT_menu_second_line_write(0, "Quick Setup");
    TFT_menu_Arrow_write("<-");
    ((UI_Menu_Parameter_t *)g_UIManager.current_screen->Parameter)->Menu_ui_Process = 0;
}

void FSM_Menu_handle_state_while(void)
{
    char *Cursor_Buff[] = {"Quick Setup", // FSM_Quick_Setup
                           "Configure",   // FSM_Configure
                           "System",      // FSM_System
                           "Service"};    // FSM_Service

    int16_t Menu_ui_Process = ((UI_Menu_Parameter_t *)g_UIManager.current_screen->Parameter)->Menu_ui_Process;

    if (Loop_flag)
    {
        TFT_menu_second_line_write(0, Cursor_Buff[Menu_ui_Process]);
        Loop_flag = false;
    }
    else
    {
        TFT_menu_second_line_Clear(0, 265);
        Loop_flag = true;
    }
}

void FSM_Menu_handle_event(FSM_Event_t event)
{
    switch (event)
    {
    case FSM_Event_UP_PRESS_UP:
        FSM_Menu_Event_UP_PRESS_UP_handler();
        break;
    case FSM_Event_DOWN_PRESS_UP:
        FSM_Menu_Event_DOWN_PRESS_UP_handler();
        break;
    case FSM_Event_LEFT_PRESS_UP:
        FSM_Menu_Event_LEFT_PRESS_UP_handler();
        break;
    case FSM_Event_RIGHT_PRESS_UP:
        FSM_Menu_Event_RIGHT_PRESS_UP_handler();
        break;
    case FSM_Event_ENTER_PRESS_UP:
        FSM_Menu_Event_ENTER_PRESS_UP_handler();
        break;
    default:
        break;
    }
}

void FSM_Menu_struct_destroy(void)
{
}

void FSM_Cal_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_first_line_write(0, "Calibrate Analog");
    TFT_menu_second_line_write(0, "Analog Output 1");
    TFT_menu_Arrow_write("<-");
    ((UI_Cal_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Aout_Num = 1;
    g_UIManager.current_screen->Cursor = 0;
}
void FSM_Cal_handle_state_while(void)
{

    int UI_Cal_Aout_Num = ((UI_Cal_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Aout_Num;

    if (Loop_flag)
    {
        if (g_UIManager.current_screen->Cursor == -1)
        {
            TFT_menu_Arrow_write("<-");
        }
        else
        {
            tft.fillRect(TFT_UI_START_X + 14 * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
            tft.drawChar(UI_Cal_Aout_Num + '0', TFT_UI_START_X + 14 * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, tft.fontcolor);
        }
        Loop_flag = false;
    }
    else
    {
        if (g_UIManager.current_screen->Cursor == -1)
        {
            TFT_menu_Arrow_Clear();
        }
        else
        {
            tft.fillRect(TFT_UI_START_X + 14 * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
            tft.drawChar('_', TFT_UI_START_X + 14 * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, tft.fontcolor);
        }
        Loop_flag = true;
    }
}
void FSM_Cal_handle_event(FSM_Event_t event)
{
    switch (event)
    {
    case FSM_Event_UP_PRESS_UP:
        FSM_Cal_Event_UP_PRESS_UP_handler();
        break;
    case FSM_Event_DOWN_PRESS_UP:
        FSM_Cal_Event_DOWN_PRESS_UP_handler();
        break;
    case FSM_Event_LEFT_PRESS_UP:
        FSM_Cal_Event_LEFT_PRESS_UP_handler();
        break;
    case FSM_Event_RIGHT_PRESS_UP:
        FSM_Cal_Event_RIGHT_PRESS_UP_handler();
        break;
    case FSM_Event_ENTER_PRESS_UP:
        FSM_Cal_Event_ENTER_PRESS_UP_handler();
        break;
    default:
        break;
    }
}
void FSM_Cal_struct_destroy(void) {}

void FSM_Cal_2_struct_create(void)
{
    g_FSMData_temp.Cal_Aout_Num = g_UI_Parameter.Cal_Parameter.UI_Cal_Aout_Num;

    g_UIManager.current_screen->Cursor = 0;
    if (g_FSMData_temp.Cal_Aout_Num == 1)
    {
        ((UI_Cal_2_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter = g_FSMData.Cal_Aout1_4mA;
    }
    else
    {
        ((UI_Cal_2_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter = g_FSMData.Cal_Aout2_4mA;
    }

    TFT_menu_ALL_Clear();
    sprintf(str, "Aout%d 4mA Set %05d", g_FSMData_temp.Cal_Aout_Num, ((UI_Cal_2_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter);
    TFT_menu_first_line_write(0, str);
    TFT_menu_second_line_write(0, "Press ENTER when Done");
    TFT_menu_Arrow_write("<-");
}
void FSM_Cal_2_handle_state_while(void)
{
    if (Loop_flag)
    {
        if (g_UIManager.current_screen->Cursor == -1)
        {
            TFT_menu_Arrow_write("<-");
        }
        else
        {
            // const int power_of_10[] = {1, 10, 100, 1000, 10000};
            int temp = ((UI_Cal_2_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter / power_of_10[4 - g_UIManager.current_screen->Cursor] % 10;

            tft.fillRect(TFT_UI_START_X + (14 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
            tft.drawChar(temp + '0', TFT_UI_START_X + (14 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
        }
        Loop_flag = false;
    }
    else
    {
        if (g_UIManager.current_screen->Cursor == -1)
        {
            TFT_menu_Arrow_Clear();
        }
        else
        {
            tft.fillRect(TFT_UI_START_X + (14 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
            tft.drawChar('_', TFT_UI_START_X + (14 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
        }
        Loop_flag = true;
    }
}
void FSM_Cal_2_handle_event(FSM_Event_t event)
{
    switch (event)
    {
    case FSM_Event_UP_PRESS_UP:
        FSM_Cal_2_Event_UP_PRESS_UP_handler();
        break;
    case FSM_Event_DOWN_PRESS_UP:
        FSM_Cal_2_Event_DOWN_PRESS_UP_handler();
        break;
    case FSM_Event_LEFT_PRESS_UP:
        FSM_Cal_2_Event_LEFT_PRESS_UP_handler();
        break;
    case FSM_Event_RIGHT_PRESS_UP:
        FSM_Cal_2_Event_RIGHT_PRESS_UP_handler();
        break;
    case FSM_Event_ENTER_PRESS_UP:
        FSM_Cal_2_Event_ENTER_PRESS_UP_handler();
        break;
    default:
        break;
    }
}
void FSM_Cal_2_struct_destroy(void)
{
    if (g_FSMData_temp.Cal_Aout_Num == 1)
    {
        g_FSMData_temp.Cal_Aout1_4mA = ((UI_Cal_2_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter;
    }
    else
    {
        g_FSMData_temp.Cal_Aout2_4mA = ((UI_Cal_2_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter;
    }
}

void FSM_Cal_3_struct_create(void)
{

    if (g_FSMData_temp.Cal_Aout_Num == 1)
    {
        // g_FSMData_temp.Cal_Aout1_4mA = ((UI_Cal_3_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter;
        ((UI_Cal_3_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter = g_FSMData.Cal_Aout1_20mA;
    }
    else
    {
        // g_FSMData_temp.Cal_Aout2_4mA = ((UI_Cal_3_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter;
        ((UI_Cal_3_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter = g_FSMData.Cal_Aout2_20mA;
    }

    g_UIManager.current_screen->Cursor = 0;
    Loop_flag = true;

    TFT_menu_ALL_Clear();
    sprintf(str, "Aout%d 20mA Set %05d", g_FSMData_temp.Cal_Aout_Num, ((UI_Cal_3_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter);
    TFT_menu_first_line_write(0, str);
    TFT_menu_second_line_write(0, "Press ENTER when Done");
    TFT_menu_Arrow_write("<-");
}
void FSM_Cal_3_handle_state_while(void)
{
    if (Loop_flag)
    {
        if (g_UIManager.current_screen->Cursor == -1)
        {
            TFT_menu_Arrow_write("<-");
        }
        else
        {
            // const int power_of_10[] = {1, 10, 100, 1000, 10000};
            int temp = ((UI_Cal_2_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter / power_of_10[4 - g_UIManager.current_screen->Cursor] % 10;

            tft.fillRect(TFT_UI_START_X + (15 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
            tft.drawChar(temp + '0', TFT_UI_START_X + (15 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
        }
        Loop_flag = false;
    }
    else
    {
        if (g_UIManager.current_screen->Cursor == -1)
        {
            TFT_menu_Arrow_Clear();
        }
        else
        {
            tft.fillRect(TFT_UI_START_X + (15 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH, TFT_TEXT_LENGTH, 0);
            tft.drawChar('_', TFT_UI_START_X + (15 + g_UIManager.current_screen->Cursor) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
        }
        Loop_flag = true;
    }
}
void FSM_Cal_3_handle_event(FSM_Event_t event)
{

    switch (event)
    {
    case FSM_Event_UP_PRESS_UP:
        FSM_Cal_3_Event_UP_PRESS_UP_handler();
        break;
    case FSM_Event_DOWN_PRESS_UP:
        FSM_Cal_3_Event_DOWN_PRESS_UP_handler();
        break;
    case FSM_Event_LEFT_PRESS_UP:
        FSM_Cal_3_Event_LEFT_PRESS_UP_handler();
        break;
    case FSM_Event_RIGHT_PRESS_UP:
        FSM_Cal_3_Event_RIGHT_PRESS_UP_handler();
        break;
    case FSM_Event_ENTER_PRESS_UP:
        FSM_Cal_3_Event_ENTER_PRESS_UP_handler();
        break;
    default:
        break;
    }
}
void FSM_Cal_3_struct_destroy(void)
{
    if (g_FSMData_temp.Cal_Aout_Num == 1)
    {
        g_FSMData_temp.Cal_Aout1_20mA = ((UI_Cal_3_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter;
    }
    else
    {
        g_FSMData_temp.Cal_Aout2_20mA = ((UI_Cal_3_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter;
    }
}

void FSM_Cal_4_struct_create(void)
{

    g_UIManager.current_screen->Cursor = 0;
    ((UI_Cal_4_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter = 0;
    Loop_flag = true;
    TFT_menu_ALL_Clear();

    TFT_menu_first_line_write(0, "Save Calibration Yes");
    TFT_menu_second_line_write(0, "Press ENTER to Exit");
    TFT_menu_Arrow_write("<-");
}
void FSM_Cal_4_handle_state_while(void)
{
    if (Loop_flag)
    {
        if (g_UIManager.current_screen->Cursor == -1)
        {
            TFT_menu_Arrow_write("<-");
        }
        else
        {
            // const int power_of_10[] = {1, 10, 100, 1000, 10000};
            //                    int temp = (g_FSMGlobal.Cal_ui_Parameter / power_of_10[4 - g_FSMGlobal.Cal_ui_Cursor]) % 10;

            tft.fillRect(TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH * 3, TFT_TEXT_LENGTH, 0);
            if (((UI_Cal_4_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter == 0)
            {
                tft.drawString("Yes", TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
            }
            else
            {
                tft.drawString("No", TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y);
            }
        }
        Loop_flag = false;
    }
    else
    {
        if (g_UIManager.current_screen->Cursor == -1)
        {
            TFT_menu_Arrow_Clear();
        }
        else
        {
            tft.fillRect(TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y, TFT_TEXT_WIDTH * 3, TFT_TEXT_LENGTH, 0);
            tft.drawChar('_', TFT_UI_START_X + (17) * TFT_TEXT_WIDTH, TFT_UI_START_Y, tft.fontcolor);
        }
        Loop_flag = true;
    }
}
void FSM_Cal_4_handle_event(FSM_Event_t event)
{
    switch (event)
    {
    case FSM_Event_UP_PRESS_UP:
        FSM_Cal_4_Event_UP_PRESS_UP_handler();
        break;
    case FSM_Event_DOWN_PRESS_UP:
        FSM_Cal_4_Event_DOWN_PRESS_UP_handler();
        break;
    case FSM_Event_LEFT_PRESS_UP:
        FSM_Cal_4_Event_LEFT_PRESS_UP_handler();
        break;
    case FSM_Event_RIGHT_PRESS_UP:
        FSM_Cal_4_Event_RIGHT_PRESS_UP_handler();
        break;
    case FSM_Event_ENTER_PRESS_UP:
        FSM_Cal_4_Event_ENTER_PRESS_UP_handler();
        break;
    default:
        break;
    }
}
void FSM_Cal_4_struct_destroy(void) {}

void FSM_Cal_5_struct_create(void)
{
    g_UIManager.current_screen->Cursor = 0;
    ((UI_Cal_5_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Cal_Parameter = 0;
    Loop_flag = true;
    TFT_menu_ALL_Clear();

    TFT_menu_first_line_write(0, "Calibration Successful");
    TFT_menu_second_line_write(0, "Press ENTER to Continue");
}
void FSM_Cal_5_handle_state_while(void) {}
void FSM_Cal_5_handle_event(FSM_Event_t event)
{
    switch (event)
    {
    case FSM_Event_ENTER_PRESS_UP:
        FSM_Cal_5_Event_ENTER_PRESS_UP_handler();
        break;
    default:
        break;
    }
}
void FSM_Cal_5_struct_destroy(void) {}

void FSM_Info_struct_create(void) {}
void FSM_Info_handle_state_while(void) {}
void FSM_Info_handle_event(FSM_Event_t event) {}
void FSM_Info_struct_destroy(void) {}

void FSM_Quick_Setup_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_second_line_write(0, "Parameter = TOC");
    TFT_menu_Arrow_write("<-");
}
void FSM_Quick_Setup_handle_state_while(void)
{
    if (Loop_flag)
    {
        if (g_UIManager.current_screen->Cursor == -1)
        {
            TFT_menu_Arrow_write("<-");
        }
        else
        {

            // TFT_menu_first_line_write(0,FSM_State_Strings[g_FSMGlobal.Now_State]);
            // tft.fillRect(TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);
            //  tft.drawString("TOC", TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH);
            Menu_ClearChar(2, 12, 3);
            Menu_WriteStr(2, 12, "TOC");
            // TFT_menu_second_line_write(TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, "TOC");
            //  TFT_menu_Arrow_write("->");
        }
        Loop_flag = false;
    }
    else
    {
        if (g_UIManager.current_screen->Cursor == -1)
        {
            TFT_menu_Arrow_Clear();
        }
        else
        {
            // tft.fillRect(TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);
            // tft.drawChar('_', TFT_UI_START_X + (12) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, tft.fontcolor);
            Menu_ClearChar(2, 12, 3);
            Menu_WriteStr(2, 12, "_");
        }
        Loop_flag = true;
    }
}
void FSM_Quick_Setup_handle_event(FSM_Event_t event)
{
    switch (event)
    {
    case FSM_Event_UP_PRESS_UP:
        FSM_Quick_Setup_Event_UP_PRESS_UP_handler();
        break;
    case FSM_Event_DOWN_PRESS_UP:
        FSM_Quick_Setup_Event_DOWN_PRESS_UP_handler();
        break;
    case FSM_Event_LEFT_PRESS_UP:
        FSM_Quick_Setup_Event_LEFT_PRESS_UP_handler();
        break;
    case FSM_Event_RIGHT_PRESS_UP:
        FSM_Quick_Setup_Event_RIGHT_PRESS_UP_handler();
        break;
    case FSM_Event_ENTER_PRESS_UP:
        FSM_Quick_Setup_Event_ENTER_PRESS_UP_handler();
        break;
    default:
        break;
    }
}
void FSM_Quick_Setup_struct_destroy(void)
{
}

void FSM_Quick_Setup_2_struct_create(void)
{

    g_UIManager.current_screen->Cursor = 0;
    ((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter = 0;
    ((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter_2 = 0;
    ((UI_Quick_Setup_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Quick_Setup_Parameter_3 = 0;

    Loop_flag = true;
    TFT_menu_ALL_Clear();

    TFT_menu_first_line_write(0, "a ");
    Celsius_display((2) * TFT_TEXT_WIDTH, 1);

    TFT_menu_second_line_write(0, "Analog Output? Yes");
    TFT_menu_Arrow_write("<-");
}
void FSM_Quick_Setup_2_handle_state_while(void)
{
    if (Loop_flag)
    {
        Quick_Analog_draw(g_UIManager.current_screen->Cursor);
        Loop_flag = false;
    }
    else
    {
        switch (g_UIManager.current_screen->Cursor)
        {
        case 0:
            tft.fillRect(TFT_UI_START_X, TFT_UI_START_Y, TFT_TEXT_WIDTH, 20, 0);
            tft.drawString("_", TFT_UI_START_X, TFT_UI_START_Y);

            break;
        case 1:
            TFT_menu_first_line_Clear((2) * TFT_TEXT_WIDTH, 10 * TFT_TEXT_WIDTH);

            // tft.fillRect(TFT_UI_START_X + (2) * TFT_TEXT_WIDTH, TFT_UI_START_Y, 2 * TFT_TEXT_WIDTH, 20, 0);
            tft.drawString("_", TFT_UI_START_X + (2) * TFT_TEXT_WIDTH, TFT_UI_START_Y);

            break;
        case 2:
            tft.fillRect(TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH, 3 * TFT_TEXT_WIDTH, 20, 0);
            tft.drawString("_", TFT_UI_START_X + (15) * TFT_TEXT_WIDTH, TFT_UI_START_Y + TFT_TEXT_LENGTH);
            break;
        case -1:
            TFT_menu_Arrow_Clear();
            break;

        default:
            break;
        }
        Loop_flag = true;
    }
}
void FSM_Quick_Setup_2_handle_event(FSM_Event_t event)
{
    switch (event)
    {
    case FSM_Event_UP_PRESS_UP:
        FSM_Quick_Setup_2_Event_UP_PRESS_UP_handler();
        break;
    case FSM_Event_DOWN_PRESS_UP:
        FSM_Quick_Setup_2_Event_DOWN_PRESS_UP_handler();
        break;
    case FSM_Event_LEFT_PRESS_UP:
        FSM_Quick_Setup_2_Event_LEFT_PRESS_UP_handler();
        break;
    case FSM_Event_RIGHT_PRESS_UP:
        FSM_Quick_Setup_2_Event_RIGHT_PRESS_UP_handler();
        break;
    case FSM_Event_ENTER_PRESS_UP:
        FSM_Quick_Setup_2_Event_ENTER_PRESS_UP_handler();
        break;
    default:
        break;
    }
}
void FSM_Quick_Setup_2_struct_destroy(void) {}

void FSM_Quick_Setup_3_struct_create(void)
{
}
void FSM_Quick_Setup_3_handle_state_while(void)
{
}
void FSM_Quick_Setup_3_handle_event(FSM_Event_t event) {
    switch (event)
    {
    case FSM_Event_UP_PRESS_UP:
        FSM_Quick_Setup_3_Event_UP_PRESS_UP_handler();
        break;
    case FSM_Event_DOWN_PRESS_UP:
        FSM_Quick_Setup_3_Event_DOWN_PRESS_UP_handler();
        break;
    case FSM_Event_LEFT_PRESS_UP:
        FSM_Quick_Setup_3_Event_LEFT_PRESS_UP_handler();
        break;
    case FSM_Event_RIGHT_PRESS_UP:
        FSM_Quick_Setup_3_Event_RIGHT_PRESS_UP_handler();
        break;
    case FSM_Event_ENTER_PRESS_UP:
        FSM_Quick_Setup_3_Event_ENTER_PRESS_UP_handler();
        break;
    default:
        break;
    }
}
void FSM_Quick_Setup_3_struct_destroy(void) {}

void FSM_Configure_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_first_line_write(0, "Configure");
    TFT_menu_second_line_write(0, "Measurement");
    TFT_menu_Arrow_write("<-");
}
void FSM_Configure_handle_state_while(void)
{
    char *Cursor_Buff[] = {
        "Measurement",    // FSM_Measurement
        "Analog Outputs", // FSM_Analog_Outputs
        "Set Points",     // FSM_Set_Points
        "Alarm",          // FSM_Alarm
        "TOC Setup",      // FSM_TOC_Setup
        "Display",        // FSM_Display
        "Flow Control",   // FSM_Flow_Contro1
        "Hold outputs",   // FSM_Hold_outputs
    };

    int16_t Temp_ui_Process = ((UI_Configure_Parameter_t *)g_UIManager.current_screen->Parameter)->UI_Configure_Parameter;
    if (Loop_flag)
    {
        TFT_menu_second_line_write(0, Cursor_Buff[Temp_ui_Process]);
        Loop_flag = false;
    }
    else
    {
        TFT_menu_second_line_Clear(0, 265);
        Loop_flag = true;
    }
}
void FSM_Configure_handle_event(FSM_Event_t event)
{
    switch (event)
    {
    case FSM_Event_UP_PRESS_UP:
        FSM_Configure_Event_UP_PRESS_UP_handler();
        break;
    case FSM_Event_DOWN_PRESS_UP:
        FSM_Configure_Event_DOWN_PRESS_UP_handler();
        break;
    case FSM_Event_LEFT_PRESS_UP:
        FSM_Configure_Event_LEFT_PRESS_UP_handler();
        break;
    case FSM_Event_RIGHT_PRESS_UP:
        FSM_Configure_Event_RIGHT_PRESS_UP_handler();
        break;
    case FSM_Event_ENTER_PRESS_UP:
        FSM_Configure_Event_ENTER_PRESS_UP_handler();
        break;
    default:
        break;
    }
}
void FSM_Configure_struct_destroy(void) {}

void FSM_System_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_first_line_write(0, "System");
    TFT_menu_second_line_write(0, "Set Language");
    TFT_menu_Arrow_write("<-");
}
void FSM_System_handle_state_while(void) {}
void FSM_System_handle_event(FSM_Event_t event) {}
void FSM_System_struct_destroy(void) {}

void FSM_Service_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_first_line_write(0, "Service");
    TFT_menu_second_line_write(0, "Diagnostic");
    TFT_menu_Arrow_write("<-");
}
void FSM_Service_handle_state_while(void) {}
void FSM_Service_handle_event(FSM_Event_t event) {}
void FSM_Service_struct_destroy(void) {}

void FSM_Messages_struct_create(void) {}
void FSM_Messages_handle_state_while(void) {}
void FSM_Messages_handle_event(FSM_Event_t event) {}
void FSM_Messages_struct_destroy(void) {}

void FSM_Calibration_Data_struct_create(void) {}
void FSM_Calibration_Data_handle_state_while(void) {}
void FSM_Calibration_Data_handle_event(FSM_Event_t event) {}
void FSM_Calibration_Data_struct_destroy(void) {}

void FSM_Model_Software_Revision_struct_create(void) {}
void FSM_Model_Software_Revision_handle_state_while(void) {}
void FSM_Model_Software_Revision_handle_event(FSM_Event_t event) {}
void FSM_Model_Software_Revision_struct_destroy(void) {}

void FSM_TOC_Sensor_Info_struct_create(void) {}
void FSM_TOC_Sensor_Info_handle_state_while(void) {}
void FSM_TOC_Sensor_Info_handle_event(FSM_Event_t event) {}
void FSM_TOC_Sensor_Info_struct_destroy(void) {}

void FSM_Measurement_struct_create(void);
void FSM_Measurement_handle_state_while(void);
void FSM_Measurement_handle_event(FSM_Event_t event);
void FSM_Measurement_struct_destroy(void);

void FSM_Analog_Outputs_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_first_line_write(0, "Aout1 Measurement = a");
    TFT_menu_second_line_write(0, "Aout2 RanGe = 4-20");
    TFT_menu_Arrow_write("<-");
}
void FSM_Analog_Outputs_handle_state_while(void) {}
void FSM_Analog_Outputs_handle_event(FSM_Event_t event) {}
void FSM_Analog_Outputs_struct_destroy(void) {}

void FSM_Set_Points_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_first_line_write(0, "SP1 on Measurement a");
    TFT_menu_second_line_write(0, "SP1 Type = OFF");
    TFT_menu_Arrow_write("<-");
}
void FSM_Set_Points_handle_state_while(void) {}
void FSM_Set_Points_handle_event(FSM_Event_t event) {}
void FSM_Set_Points_struct_destroy(void) {}

void FSM_Alarm_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_first_line_write(0, "Setup Alarm");
    TFT_menu_second_line_write(0, "Use Relay # 2");
    TFT_menu_Arrow_write("<-");
}
void FSM_Alarm_handle_state_while(void) {}
void FSM_Alarm_handle_event(FSM_Event_t event) {}
void FSM_Alarm_struct_destroy(void) {}

void FSM_TOC_Setup_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_first_line_write(0, "Alarm");
    TFT_menu_second_line_write(0, "TOC Setup");
    TFT_menu_Arrow_write("<-");
}
void FSM_TOC_Setup_handle_state_while(void) {}
void FSM_TOC_Setup_handle_event(FSM_Event_t event) {}
void FSM_TOC_Setup_struct_destroy(void) {}

void FSM_Display_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_first_line_write(0, "Display Setup");
    TFT_menu_second_line_write(0, "Measurement");
    TFT_menu_Arrow_write("<-");
}
void FSM_Display_handle_state_while(void) {}
void FSM_Display_handle_event(FSM_Event_t event) {}
void FSM_Display_struct_destroy(void) {}

void FSM_Flow_Control_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_second_line_write(0, "Accessory Mode = off");
    TFT_menu_Arrow_write("<-");
}
void FSM_Flow_Control_handle_state_while(void) {}
void FSM_Flow_Control_handle_event(FSM_Event_t event) {}
void FSM_Flow_Control_struct_destroy(void) {}

void FSM_Hold_outputs_struct_create(void)
{
    TFT_menu_ALL_Clear();
    TFT_menu_first_line_write(0, "Hold Outputs?  Yes");
    TFT_menu_Arrow_write("<-");
}
void FSM_Hold_outputs_handle_state_while(void) {}
void FSM_Hold_outputs_handle_event(FSM_Event_t event) {}
void FSM_Hold_outputs_struct_destroy(void) {}

void FSM_Set_Language_struct_create(void) {}
void FSM_Set_Language_handle_state_while(void) {}
void FSM_Set_Language_handle_event(FSM_Event_t event) {}
void FSM_Set_Language_struct_destroy(void) {}

void FSM_USB_struct_create(void) {}
void FSM_USB_handle_state_while(void) {}
void FSM_USB_handle_event(FSM_Event_t event) {}
void FSM_USB_struct_destroy(void) {}

void FSM_Passwords_struct_create(void) {}
void FSM_Passwords_handle_state_while(void) {}
void FSM_Passwords_handle_event(FSM_Event_t event) {}
void FSM_Passwords_struct_destroy(void) {}

void FSM_Set_Clear_Lockout_struct_create(void) {}
void FSM_Set_Clear_Lockout_handle_state_while(void) {}
void FSM_Set_Clear_Lockout_handle_event(FSM_Event_t event) {}
void FSM_Set_Clear_Lockout_struct_destroy(void) {}

void FSM_Reset_struct_create(void) {}
void FSM_Reset_handle_state_while(void) {}
void FSM_Reset_handle_event(FSM_Event_t event) {}
void FSM_Reset_struct_destroy(void) {}

void FSM_Diagnostic_struct_create(void) {}
void FSM_Diagnostic_handle_state_while(void) {}
void FSM_Diagnostic_handle_event(FSM_Event_t event) {}
void FSM_Diagnostic_struct_destroy(void) {}

void FSM_Calibrate_struct_create(void) {}
void FSM_Calibrate_handle_state_while(void) {}
void FSM_Calibrate_handle_event(FSM_Event_t event) {}
void FSM_Calibrate_struct_destroy(void) {}

void FSM_Tech_Service_struct_create(void) {}
void FSM_Tech_Service_handle_state_while(void) {}
void FSM_Tech_Service_handle_event(FSM_Event_t event) {}
void FSM_Tech_Service_struct_destroy(void) {}

void FSM_Channel_Setup_struct_create(void);
void FSM_Channel_Setup_handle_state_while(void);
void FSM_Channel_Setup_handle_event(FSM_Event_t event);
void FSM_Channel_Setup_struct_destroy(void);

void FSM_Channel_Setup_1_struct_create(void);
void FSM_Channel_Setup_1_handle_state_while(void);
void FSM_Channel_Setup_1_handle_event(FSM_Event_t event);
void FSM_Channel_Setup_1_struct_destroy(void);

void FSM_Resistivity_struct_create(void) {}
void FSM_Resistivity_handle_state_while(void) {}
void FSM_Resistivity_handle_event(FSM_Event_t event) {}
void FSM_Resistivity_struct_destroy(void) {}

void FSM_Set_Averaging_struct_create(void) {}
void FSM_Set_Averaging_handle_state_while(void) {}
void FSM_Set_Averaging_handle_event(FSM_Event_t event) {}
void FSM_Set_Averaging_struct_destroy(void) {}

void TFT_UIManager_init(void)
{
    // 设置初始界面
    g_UIManager.current_screen = &g_UI_process_t[FSM_NONE];
    g_UIManager.current_screen_id = FSM_NONE;
}

UI_process_t g_UI_process_t[FSM_UI_COUNT] = {
    {FSM_NONE, &g_UI_Parameter.None_Parameter, 0, FSM_NONE_struct_create, FSM_NONE_handle_state_while, FSM_NONE_handle_event, FSM_NONE_struct_destroy},
    {FSM_Menu, &g_UI_Parameter.Menu_Parameter, 0, FSM_Menu_struct_create, FSM_Menu_handle_state_while, FSM_Menu_handle_event, FSM_Menu_struct_destroy},
    {FSM_Cal, &g_UI_Parameter.Cal_Parameter, 0, FSM_Cal_struct_create, FSM_Cal_handle_state_while, FSM_Cal_handle_event, FSM_Cal_struct_destroy},
    {FSM_Cal_2, &g_UI_Parameter.Cal_Parameter_2, 0, FSM_Cal_2_struct_create, FSM_Cal_2_handle_state_while, FSM_Cal_2_handle_event, FSM_Cal_2_struct_destroy},
    {FSM_Cal_3, &g_UI_Parameter.Cal_Parameter_3, 0, FSM_Cal_3_struct_create, FSM_Cal_3_handle_state_while, FSM_Cal_3_handle_event, FSM_Cal_3_struct_destroy},
    {FSM_Cal_4, &g_UI_Parameter.Cal_Parameter_4, 0, FSM_Cal_4_struct_create, FSM_Cal_4_handle_state_while, FSM_Cal_4_handle_event, FSM_Cal_4_struct_destroy},
    {FSM_Cal_5, &g_UI_Parameter.Cal_Parameter_5, 0, FSM_Cal_5_struct_create, FSM_Cal_5_handle_state_while, FSM_Cal_5_handle_event, FSM_Cal_5_struct_destroy},
    {FSM_Info, &g_UI_Parameter.Info_Parameter, 0, FSM_Info_struct_create, FSM_Info_handle_state_while, FSM_Info_handle_event, FSM_Info_struct_destroy},
    {FSM_Quick_Setup, &g_UI_Parameter.Quick_Setup_Parameter, 0, FSM_Quick_Setup_struct_create, FSM_Quick_Setup_handle_state_while, FSM_Quick_Setup_handle_event, FSM_Quick_Setup_struct_destroy},
    {FSM_Quick_Setup_2, &g_UI_Parameter.Quick_Setup_Parameter_2, 0, FSM_Quick_Setup_2_struct_create, FSM_Quick_Setup_2_handle_state_while, FSM_Quick_Setup_2_handle_event, FSM_Quick_Setup_2_struct_destroy},
    {FSM_Quick_Setup_3, &g_UI_Parameter.Quick_Setup_Parameter_3, 0, FSM_Quick_Setup_3_struct_create, FSM_Quick_Setup_3_handle_state_while, FSM_Quick_Setup_3_handle_event, FSM_Quick_Setup_3_struct_destroy},
    {FSM_Configure, &g_UI_Parameter.Configure_Parameter, 0, FSM_Configure_struct_create, FSM_Configure_handle_state_while, FSM_Configure_handle_event, FSM_Configure_struct_destroy},
    {FSM_System, &g_UI_Parameter.System_Parameter, 0, FSM_System_struct_create, FSM_System_handle_state_while, FSM_System_handle_event, FSM_System_struct_destroy},
    {FSM_Service, &g_UI_Parameter.Service_Parameter, 0, FSM_Service_struct_create, FSM_Service_handle_state_while, FSM_Service_handle_event, FSM_Service_struct_destroy},
    {FSM_Messages, &g_UI_Parameter.Messages_Parameter, 0, FSM_Messages_struct_create, FSM_Messages_handle_state_while, FSM_Messages_handle_event, FSM_Messages_struct_destroy},
    {FSM_Calibration_Data, &g_UI_Parameter.Calibration_Data_Parameter, 0, FSM_Calibration_Data_struct_create, FSM_Calibration_Data_handle_state_while, FSM_Calibration_Data_handle_event, FSM_Calibration_Data_struct_destroy},
    {FSM_Model_Software_Revision, &g_UI_Parameter.Model_Software_Revision_Parameter, 0, FSM_Model_Software_Revision_struct_create, FSM_Model_Software_Revision_handle_state_while, FSM_Model_Software_Revision_handle_event, FSM_Model_Software_Revision_struct_destroy},
    {FSM_TOC_Sensor_Info, &g_UI_Parameter.TOC_Sensor_Info_Parameter, 0, FSM_TOC_Sensor_Info_struct_create, FSM_TOC_Sensor_Info_handle_state_while, FSM_TOC_Sensor_Info_handle_event, FSM_TOC_Sensor_Info_struct_destroy},
    {FSM_Measurement, &g_UI_Parameter.Measurement_Parameter, 0, FSM_Measurement_struct_create, FSM_Measurement_handle_state_while, FSM_Measurement_handle_event, FSM_Measurement_struct_destroy},
    {FSM_Analog_Outputs, &g_UI_Parameter.Analog_Outputs_Parameter, 0, FSM_Analog_Outputs_struct_create, FSM_Analog_Outputs_handle_state_while, FSM_Analog_Outputs_handle_event, FSM_Analog_Outputs_struct_destroy},
    {FSM_Set_Points, &g_UI_Parameter.Set_Points_Parameter, 0, FSM_Set_Points_struct_create, FSM_Set_Points_handle_state_while, FSM_Set_Points_handle_event, FSM_Set_Points_struct_destroy},
    {FSM_Alarm, &g_UI_Parameter.Alarm_Parameter, 0, FSM_Alarm_struct_create, FSM_Alarm_handle_state_while, FSM_Alarm_handle_event, FSM_Alarm_struct_destroy},
    {FSM_TOC_Setup, &g_UI_Parameter.TOC_Setup_Parameter, 0, FSM_TOC_Setup_struct_create, FSM_TOC_Setup_handle_state_while, FSM_TOC_Setup_handle_event, FSM_TOC_Setup_struct_destroy},
    {FSM_Display, &g_UI_Parameter.Display_Parameter, 0, FSM_Display_struct_create, FSM_Display_handle_state_while, FSM_Display_handle_event, FSM_Display_struct_destroy},
    {FSM_Flow_Control, &g_UI_Parameter.Flow_Control_Parameter, 0, FSM_Flow_Control_struct_create, FSM_Flow_Control_handle_state_while, FSM_Flow_Control_handle_event, FSM_Flow_Control_struct_destroy},
    {FSM_Hold_outputs, &g_UI_Parameter.Hold_outputs_Parameter, 0, FSM_Hold_outputs_struct_create, FSM_Hold_outputs_handle_state_while, FSM_Hold_outputs_handle_event, FSM_Hold_outputs_struct_destroy},
    {FSM_Set_Language, &g_UI_Parameter.Set_Language_Parameter, 0, FSM_Set_Language_struct_create, FSM_Set_Language_handle_state_while, FSM_Set_Language_handle_event, FSM_Set_Language_struct_destroy},
    {FSM_USB, &g_UI_Parameter.USB_Parameter, 0, FSM_USB_struct_create, FSM_USB_handle_state_while, FSM_USB_handle_event, FSM_USB_struct_destroy},
    {FSM_Passwords, &g_UI_Parameter.Passwords_Parameter, 0, FSM_Passwords_struct_create, FSM_Passwords_handle_state_while, FSM_Passwords_handle_event, FSM_Passwords_struct_destroy},
    {FSM_Set_Clear_Lockout, &g_UI_Parameter.Set_Clear_Lockout_Parameter, 0, FSM_Set_Clear_Lockout_struct_create, FSM_Set_Clear_Lockout_handle_state_while, FSM_Set_Clear_Lockout_handle_event, FSM_Set_Clear_Lockout_struct_destroy},
    {FSM_Reset, &g_UI_Parameter.Reset_Parameter, 0, FSM_Reset_struct_create, FSM_Reset_handle_state_while, FSM_Reset_handle_event, FSM_Reset_struct_destroy},
    {FSM_Diagnostic, &g_UI_Parameter.Diagnostic_Parameter, 0, FSM_Diagnostic_struct_create, FSM_Diagnostic_handle_state_while, FSM_Diagnostic_handle_event, FSM_Diagnostic_struct_destroy},
    {FSM_Calibrate, &g_UI_Parameter.Calibrate_Parameter, 0, FSM_Calibrate_struct_create, FSM_Calibrate_handle_state_while, FSM_Calibrate_handle_event, FSM_Calibrate_struct_destroy},
    {FSM_Tech_Service, &g_UI_Parameter.Tech_Service_Parameter, 0, FSM_Tech_Service_struct_create, FSM_Tech_Service_handle_state_while, FSM_Tech_Service_handle_event, FSM_Tech_Service_struct_destroy},
    {FSM_Channel_Setup, &g_UI_Parameter.Channel_Setup_Parameter, 0, FSM_Channel_Setup_struct_create, FSM_Channel_Setup_handle_state_while, FSM_Channel_Setup_handle_event, FSM_Channel_Setup_struct_destroy},
    {FSM_Channel_Setup_1, &g_UI_Parameter.Channel_Setup_1_Parameter, 0, FSM_Channel_Setup_1_struct_create, FSM_Channel_Setup_1_handle_state_while, FSM_Channel_Setup_1_handle_event, FSM_Channel_Setup_1_struct_destroy},
    {FSM_Resistivity, &g_UI_Parameter.Resistivity_Parameter, 0, FSM_Resistivity_struct_create, FSM_Resistivity_handle_state_while, FSM_Resistivity_handle_event, FSM_Resistivity_struct_destroy},
    {FSM_Set_Averaging, &g_UI_Parameter.Set_Averaging_Parameter, 0, FSM_Set_Averaging_struct_create, FSM_Set_Averaging_handle_state_while, FSM_Set_Averaging_handle_event, FSM_Set_Averaging_struct_destroy}

};

void state_machine_run(void)
{
    if (g_UIManager.current_screen && g_UIManager.current_screen->handle_state_while)
    {
        g_UIManager.current_screen->handle_state_while();
    }
}

void event_machine_run(FSM_Event_t event)
{
    if (event == FSM_Event_ESC)
    {
        TFT_menu_ALL_Clear();
        g_FSMGlobal.Now_State = FSM_NONE;
        g_UIManager.current_screen_id = FSM_NONE;
        g_UIManager.current_screen = &g_UI_process_t[g_UIManager.current_screen_id];
        UI_change_run(FSM_NONE);
        return;
    }

    if (g_UIManager.current_screen && g_UIManager.current_screen->handle_event)
    {
        g_UIManager.current_screen->handle_event(event);
        printf("666666666666\r\n");
    }
}

void UI_change_run(FSM_UI next_UI_id)
{
    if (g_UIManager.current_screen && g_UIManager.current_screen->struct_destroy)
    {
        g_UIManager.current_screen->struct_destroy();
    }

    g_UIManager.current_screen_id = next_UI_id;
    g_UIManager.current_screen = &g_UI_process_t[g_UIManager.current_screen_id];
    g_UIManager.current_screen->struct_create();
}
